Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. {
  2. "metadata": {
  3. "name": "",
  4. "signature": "sha256:aa30a584da0db37f3fdffcabde402605ae2e88baa21943bec964cacd8f1f5df0"
  5. },
  6. "nbformat": 3,
  7. "nbformat_minor": 0,
  8. "worksheets": [
  9. {
  10. "cells": [
  11. {
  12. "cell_type": "markdown",
  13. "metadata": {},
  14. "source": [
  15. "An example of using the Python->R bridge to allow R to automatically deal with numpy arrays and return numpy arrays."
  16. ]
  17. },
  18. {
  19. "cell_type": "code",
  20. "collapsed": false,
  21. "input": [
  22. "from rpy2.robjects import r\n",
  23. "from rpy2.robjects.numpy2ri import numpy2ri\n",
  24. "import rpy2.robjects as ro\n",
  25. "\n",
  26. "# Automatically extend R to handle numpy array objects.\n",
  27. "ro.conversion.py2ri = numpy2ri"
  28. ],
  29. "language": "python",
  30. "metadata": {},
  31. "outputs": [],
  32. "prompt_number": 1
  33. },
  34. {
  35. "cell_type": "code",
  36. "collapsed": false,
  37. "input": [
  38. "import iris\n",
  39. "\n",
  40. "temp = iris.load_cube(iris.sample_data_path('air_temp.pp'))"
  41. ],
  42. "language": "python",
  43. "metadata": {},
  44. "outputs": [],
  45. "prompt_number": 2
  46. },
  47. {
  48. "cell_type": "code",
  49. "collapsed": false,
  50. "input": [
  51. "print temp"
  52. ],
  53. "language": "python",
  54. "metadata": {},
  55. "outputs": [
  56. {
  57. "output_type": "stream",
  58. "stream": "stdout",
  59. "text": [
  60. "air_temperature / (K) (latitude: 73; longitude: 96)\n",
  61. " Dimension coordinates:\n",
  62. " latitude x -\n",
  63. " longitude - x\n",
  64. " Scalar coordinates:\n",
  65. " forecast_period: 6477 hours, bound=(-28083.0, 6477.0) hours\n",
  66. " forecast_reference_time: 1998-03-01 03:00:00\n",
  67. " pressure: 1000.0 hPa\n",
  68. " time: 1998-12-01 00:00:00, bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00)\n",
  69. " Attributes:\n",
  70. " STASH: m01s16i203\n",
  71. " source: Data from Met Office Unified Model\n",
  72. " Cell methods:\n",
  73. " mean: time\n"
  74. ]
  75. }
  76. ],
  77. "prompt_number": 3
  78. },
  79. {
  80. "cell_type": "code",
  81. "collapsed": false,
  82. "input": [
  83. "print r.mean(temp.data), r.max(temp.data)"
  84. ],
  85. "language": "python",
  86. "metadata": {},
  87. "outputs": [
  88. {
  89. "output_type": "stream",
  90. "stream": "stdout",
  91. "text": [
  92. "[1] 279.9452\n",
  93. " [1] 305.4866\n",
  94. "\n"
  95. ]
  96. }
  97. ],
  98. "prompt_number": 4
  99. }
  100. ],
  101. "metadata": {}
  102. }
  103. ]
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement