Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "metadata": {
- "name": "",
- "signature": "sha256:aa30a584da0db37f3fdffcabde402605ae2e88baa21943bec964cacd8f1f5df0"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "An example of using the Python->R bridge to allow R to automatically deal with numpy arrays and return numpy arrays."
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "from rpy2.robjects import r\n",
- "from rpy2.robjects.numpy2ri import numpy2ri\n",
- "import rpy2.robjects as ro\n",
- "\n",
- "# Automatically extend R to handle numpy array objects.\n",
- "ro.conversion.py2ri = numpy2ri"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 1
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "import iris\n",
- "\n",
- "temp = iris.load_cube(iris.sample_data_path('air_temp.pp'))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 2
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "print temp"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "air_temperature / (K) (latitude: 73; longitude: 96)\n",
- " Dimension coordinates:\n",
- " latitude x -\n",
- " longitude - x\n",
- " Scalar coordinates:\n",
- " forecast_period: 6477 hours, bound=(-28083.0, 6477.0) hours\n",
- " forecast_reference_time: 1998-03-01 03:00:00\n",
- " pressure: 1000.0 hPa\n",
- " time: 1998-12-01 00:00:00, bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00)\n",
- " Attributes:\n",
- " STASH: m01s16i203\n",
- " source: Data from Met Office Unified Model\n",
- " Cell methods:\n",
- " mean: time\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "print r.mean(temp.data), r.max(temp.data)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "[1] 279.9452\n",
- " [1] 305.4866\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- }
- ],
- "metadata": {}
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement