mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-27 22:11:13 +00:00
873 lines
47 KiB
Plaintext
873 lines
47 KiB
Plaintext
|
{
|
||
|
"metadata": {
|
||
|
"name": "",
|
||
|
"signature": "sha256:68e419b336c43b3a5f99d948a5148ad6a7da83f9796fdc45c9132c236a5a43bc"
|
||
|
},
|
||
|
"nbformat": 3,
|
||
|
"nbformat_minor": 0,
|
||
|
"worksheets": [
|
||
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"[Sebastian Raschka](http://sebastianraschka.com) \n",
|
||
|
"\n",
|
||
|
"- [Open in IPython nbviewer](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/awesome_things_ipynb?create=1) \n",
|
||
|
"\n",
|
||
|
"- [Link to this IPython notebook on Github](https://github.com/rasbt/python_reference/blob/master/tutorials/awesome_things_ipynb.ipynb) \n",
|
||
|
"\n",
|
||
|
"- [Link to the GitHub Repository python_reference](https://github.com/rasbt/python_reference/)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"import time\n",
|
||
|
"import platform\n",
|
||
|
"print('Last updated: %s' %time.strftime('%d/%m/%Y'))\n",
|
||
|
"print('Created using Python', platform.python_version())"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Last updated: 27/06/2014\n",
|
||
|
"Created using Python 3.4.1\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 37
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<hr>\n",
|
||
|
"I would be happy to hear your comments and suggestions. \n",
|
||
|
"Please feel free to drop me a note via\n",
|
||
|
"[twitter](https://twitter.com/rasbt), [email](mailto:bluewoodtree@gmail.com), or [google+](https://plus.google.com/+SebastianRaschka).\n",
|
||
|
"<hr>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 1,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Awesome things that you can do in IPython Notebooks (in progress)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Writing local files"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%file hello.py\n",
|
||
|
"def func_inside_script(x, y):\n",
|
||
|
" return x + y\n",
|
||
|
"print('Hello World')"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Writing hello.py\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 13
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Running Python scripts"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"We can run Python scripts in IPython via the %run magic command. For example, the Python script that we created in the [Writing local files](#Writing-local-files) section."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%run hello.py"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Hello World\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 14
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"func_inside_script(1, 2)"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"output_type": "pyout",
|
||
|
"prompt_number": 16,
|
||
|
"text": [
|
||
|
"3"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 16
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Benchmarking"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%timeit [x**2 for x in range(100)] "
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"10000 loops, best of 3: 38.8 \u00b5s per loop\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 4
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%timeit -r 5 -n 100 [x**2 for x in range(100)] "
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"100 loops, best of 5: 39 \u00b5s per loop\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 3
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Using system shell commands"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"By prepending a \"`!`\" we can conveniently execute most of the system shell commands, below are just a few examples."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"my_dir = 'new_dir'\n",
|
||
|
"!mkdir $my_dir\n",
|
||
|
"!pwd\n",
|
||
|
"!touch $my_dir'/some.txt'\n",
|
||
|
"!ls -l './new_dir'\n",
|
||
|
"!ls -l $my_dir | wc -l"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"/Users/sebastian/Desktop\r\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"total 0\r\n",
|
||
|
"-rw-r--r-- 1 sebastian staff 0 Jun 27 10:11 some.txt\r\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
" 2\r\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 12
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Debugging"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%debug \n",
|
||
|
"def some_func():\n",
|
||
|
" var = 'hello world'\n",
|
||
|
" for i in range(5):\n",
|
||
|
" print(i)\n",
|
||
|
" i / 0\n",
|
||
|
" return 'finished'"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"> \u001b[0;32m<ipython-input-1-3d5f00f75cf4>\u001b[0m(5)\u001b[0;36msome_func\u001b[0;34m()\u001b[0m\n",
|
||
|
"\u001b[0;32m 4 \u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||
|
"\u001b[0m\u001b[0;32m----> 5 \u001b[0;31m \u001b[0mi\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||
|
"\u001b[0m\u001b[0;32m 6 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'finished'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||
|
"\u001b[0m\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"ipdb> var\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"'hello world'\n"
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Inline Plotting with matplotlib"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%matplotlib inline"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"prompt_number": 33
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"import numpy as np\n",
|
||
|
"from matplotlib import pyplot as plt\n",
|
||
|
"import math\n",
|
||
|
"\n",
|
||
|
"def pdf(x, mu=0, sigma=1):\n",
|
||
|
" \"\"\"Calculates the normal distribution's probability density \n",
|
||
|
" function (PDF). \n",
|
||
|
" \n",
|
||
|
" \"\"\"\n",
|
||
|
" term1 = 1.0 / ( math.sqrt(2*np.pi) * sigma )\n",
|
||
|
" term2 = np.exp( -0.5 * ( (x-mu)/sigma )**2 )\n",
|
||
|
" return term1 * term2\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"x = np.arange(0, 100, 0.05)\n",
|
||
|
"\n",
|
||
|
"pdf1 = pdf(x, mu=5, sigma=2.5**0.5)\n",
|
||
|
"pdf2 = pdf(x, mu=10, sigma=6**0.5)\n",
|
||
|
"\n",
|
||
|
"plt.plot(x, pdf1)\n",
|
||
|
"plt.plot(x, pdf2)\n",
|
||
|
"plt.title('Probability Density Functions')\n",
|
||
|
"plt.ylabel('p(x)')\n",
|
||
|
"plt.xlabel('random variable x')\n",
|
||
|
"plt.legend(['pdf1 ~ N($\\mu=5$, $\\sigma=2.5$)', 'pdf2 ~ N($\\mu=10$, $\\sigma=6$)'], loc='upper right')\n",
|
||
|
"plt.ylim([0,0.5])\n",
|
||
|
"plt.xlim([0,20])\n",
|
||
|
"\n",
|
||
|
"plt.show()"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data",
|
||
|
"png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEZCAYAAACXRVJOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcVNX/x/HXICCioLihgIigoqKCu4YLiooram64L+Q3\nt1LLtNytLOunlYb1NXM3l8wFLUVzQa3ctVJx30DcAQUXEIb7+2N0viIoizPcGfg8e8yjuTN37n3P\nMN7PnHPvPVejKIqCEEKIfM9C7QBCCCFMgxQEIYQQgBQEIYQQT0lBEEIIAUhBEEII8ZQUBCGEEIAU\nBJEDFhYWXLp0KUevdXNzY+fOnRk+t2/fPqpUqZJm3l27dgHw2WefMWTIkByt05S0a9eO5cuXqx3D\nYOzs7Lhy5YraMYSBSEHIJ9zc3LC1tcXOzo4yZcowaNAgHj58mOs5NBoNGo0mw+eaNGnCmTNn0sz7\nzIQJE1iwYAEAV65cwcLCgtTU1BxlWLJkCQUKFMDOzg47Ozvc3d0ZPHgw58+fz9HysmPLli3069dP\nn6NJkyY5XtbAgQMpWLCg/n3Y2dmxdu1aQ0VNx8/Pj4ULF6Z5LCEhATc3N6OtU+QuKQj5hEaj4ddf\nfyUhIYFjx45x5MgRPv3003TzpaSkqJAuZ17nnEpfX18SEhKIj49nx44dFCpUiDp16nDq1CkDJjQu\njUbD+PHjSUhI0N+6d+9u1PWJvE0KQj7k5OREmzZt9Bs/CwsLvvvuOypVqoSnpycACxYsoFKlSpQo\nUYJOnTpx48aNNMv47bff8PDwoFSpUowbN06/cb548SItWrSgZMmSlCpVir59+3L//v00rz106BBe\nXl4UL16cwYMHk5SUBEB4eDjl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07l549ewL/+8Vnb29PdHQ0//d//5dm2YqiMG/ePKKj\no4mNjWXGjBnp9k9kplSpUlhYWHDx4sU0j/ft25f169fz008/0b9//ywtS6vVcvnyZd555x327t3L\n1KlT9cvavHkz27dvR6vVkpiYSHh4eJpfvC/rPtm9ezcnTpxAq9ViZ2eHlZUVBQoUSDefo6Mj165d\nIzk5Oc3j/fv354svvuDkyZO8+eabL83+svX7+PiwZcsW4uLiuHnzJt98802mn8OzZQUHB7N48WJ2\n7dpFamoq0dHR+l/rjo6O6T7zZwoUKECPHj2YOHEiDx484OrVq3z99df07ds303Xfvn2b0NBQHj58\niJWVFYULF87w8xLGJwVBpPvl5+/vzyeffELXrl1xcnLi8uXLrF69Os08nTp1ok6dOtSqVYsOHTow\nePBgAKZOncqxY8coWrQoHTt2pGvXrmmWr9Fo6NOnD61bt8bDw4NKlSoxadKkl2Z5/vFnz9na2jJx\n4kR8fX1xcHDg0KFDgO7Xbe3atbGwsKBx48avfL/Pju4pWrQozZs358GDBxw+fBgvLy9A90s9NDSU\nzz77jNKlS+Pq6srs2bPTbIRffF/Ppm/dukX37t0pWrQo1apVw8/PL8Nf+v7+/nh5eVGmTBl9/z/o\ndm5HRkbSpUsX/c7ozD6T5/Xr1w9vb2/c3Nxo06YNQUFBme5kfvZ8vXr1WLx4MWPGjKFYsWL4+fnp\nW3SjRo3il19+oXjx4owePTrdcr799lsKFy6Mu7s7TZo0oU+fPgwaNOilWZ9Np6am8vXXX+Ps7EyJ\nEiXYt28f33///UvzCuPRGPMCOWFhYYwePRqtVstbb73F+PHj0zwfHh5Op06dcHd3B6Br165pNg5C\nZFdwcDDOzs58/PHHakd5LZUqVWL+/Pm0aNFC7SgiH7E01oK1Wi0jR45kx44dODs7U69ePQIDA6la\ntWqa+Zo1a8amTZuMFUPkI1eu
|
||
|
"text": [
|
||
|
"<matplotlib.figure.Figure at 0x107e67080>"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 36
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"C-extensions via the Cython magic"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Cython (see [Cython's C-extensions for Python](http://cython.org)) is basically a hybrid between C and Python and can be pictured as compiled Python code with type declarations.\n",
|
||
|
"Since we are working in an IPython notebook here, we can make use of the very convenient IPython magic: It will take care of the conversion to C code, the compilation, and eventually the loading of the function.\n",
|
||
|
"Also, we are adding C type declarations; those type declarations are not necessary for using Cython, however, it will improve the performance of our code significantly."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%load_ext cythonmagic"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"prompt_number": 29
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%cython\n",
|
||
|
"import numpy as np\n",
|
||
|
"cimport numpy as np\n",
|
||
|
"cimport cython\n",
|
||
|
"@cython.boundscheck(False) \n",
|
||
|
"@cython.wraparound(False)\n",
|
||
|
"@cython.cdivision(True)\n",
|
||
|
"cpdef cython_lstsqr(x_ary, y_ary):\n",
|
||
|
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
|
||
|
" cdef double x_avg, y_avg, var_x, cov_xy,\\\n",
|
||
|
" slope, y_interc, temp\n",
|
||
|
" cdef double[:] x = x_ary # memoryview\n",
|
||
|
" cdef double[:] y = y_ary\n",
|
||
|
" cdef unsigned long N, i\n",
|
||
|
" \n",
|
||
|
" N = x.shape[0]\n",
|
||
|
" x_avg = 0\n",
|
||
|
" y_avg = 0\n",
|
||
|
" for i in range(N):\n",
|
||
|
" x_avg += x[i]\n",
|
||
|
" y_avg += y[i]\n",
|
||
|
" x_avg = x_avg/N\n",
|
||
|
" y_avg = y_avg/N\n",
|
||
|
" var_x = 0\n",
|
||
|
" cov_xy = 0\n",
|
||
|
" for i in range(N):\n",
|
||
|
" temp = (x[i] - x_avg)\n",
|
||
|
" var_x += temp**2\n",
|
||
|
" cov_xy += temp*(y[i] - y_avg)\n",
|
||
|
" slope = cov_xy / var_x\n",
|
||
|
" y_interc = y_avg - slope*x_avg\n",
|
||
|
" return (slope, y_interc)"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"building '_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df' extension\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"C compiler: /usr/bin/clang -fno-strict-aliasing -Werror=declaration-after-statement -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/sebastian/miniconda3/envs/py34/include -arch x86_64\n",
|
||
|
"\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"compile options: '-I/Users/sebastian/miniconda3/envs/py34/lib/python3.4/site-packages/numpy/core/include -I/Users/sebastian/miniconda3/envs/py34/include/python3.4m -c'\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"clang: /Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.c\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"/usr/bin/clang -bundle -undefined dynamic_lookup -L/Users/sebastian/miniconda3/envs/py34/lib -arch x86_64 /Users/sebastian/.ipython/cython/Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.o -L/Users/sebastian/miniconda3/envs/py34/lib -o /Users/sebastian/.ipython/cython/_cython_magic_cf6c91cb1e11de8d2dbe7d9178e469df.so\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 30
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"import numpy as np\n",
|
||
|
"\n",
|
||
|
"x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)])\n",
|
||
|
"y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)])"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"prompt_number": 31
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"cython_lstsqr(x_ary, y_ary)"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"output_type": "pyout",
|
||
|
"prompt_number": 32,
|
||
|
"text": [
|
||
|
"(1.1399825800539194, 2.0824398156005444)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 32
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Running Fortran Code"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"There is also a convenient IPython magic command for compiling Fortran code. The Fortran magic uses NumPy's [F2PY](http://www.f2py.com) module for compiling and running the Fortran code. For more information, please see the ['Fortran magic's documentation'](http://nbviewer.ipython.org/github/mgaitan/fortran_magic/blob/master/documentation.ipynb)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%install_ext https://raw.github.com/mgaitan/fortran_magic/master/fortranmagic.py"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Installed fortranmagic.py. To use it, type:\n",
|
||
|
" %load_ext fortranmagic\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 17
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%load_ext fortranmagic"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"javascript": [
|
||
|
"$.getScript(\"https://raw.github.com/marijnh/CodeMirror/master/mode/fortran/fortran.js\", function () {\n",
|
||
|
"IPython.config.cell_magic_highlight['magic_fortran'] = {'reg':[/^%%fortran/]};});\n"
|
||
|
],
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 18
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%fortran\n",
|
||
|
"SUBROUTINE fortran_lstsqr(ary_x, ary_y, slope, y_interc)\n",
|
||
|
" ! Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
|
||
|
" IMPLICIT NONE\n",
|
||
|
" REAL(8), INTENT(in), DIMENSION(:) :: ary_x, ary_y\n",
|
||
|
" REAL(8), INTENT(out) :: slope, y_interc\n",
|
||
|
" REAL(8) :: x_avg, y_avg, var_x, cov_xy, temp\n",
|
||
|
" INTEGER(8) :: N, i\n",
|
||
|
" \n",
|
||
|
" N = SIZE(ary_x)\n",
|
||
|
"\n",
|
||
|
" x_avg = SUM(ary_x) / N\n",
|
||
|
" y_avg = SUM(ary_y) / N\n",
|
||
|
" var_x = 0\n",
|
||
|
" cov_xy = 0\n",
|
||
|
" \n",
|
||
|
" DO i = 1, N\n",
|
||
|
" temp = ary_x(i) - x_avg\n",
|
||
|
" var_x = var_x + temp**2\n",
|
||
|
" cov_xy = cov_xy + (temp*(ary_y(i) - y_avg))\n",
|
||
|
" END DO\n",
|
||
|
" \n",
|
||
|
" slope = cov_xy / var_x\n",
|
||
|
" y_interc = y_avg - slope*x_avg\n",
|
||
|
"\n",
|
||
|
"END SUBROUTINE fortran_lstsqr"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"\tBuilding module \"_fortran_magic_a044885f2b0c0feac78a230b6b714e2b\"...\n",
|
||
|
"\t\tConstructing wrapper function \"fortran_lstsqr\"...\n",
|
||
|
"\t\t slope,y_interc = fortran_lstsqr(ary_x,ary_y)\n",
|
||
|
"\tWrote C/API module \"_fortran_magic_a044885f2b0c0feac78a230b6b714e2b\" to file \"/var/folders/bq/_946cdn92t7bqzz5frpfpw7r0000gp/T/tmp3y_jxtl_/src.macosx-10.5-x86_64-3.4/_fortran_magic_a044885f2b0c0feac78a230b6b714e2bmodule.c\"\n",
|
||
|
"\tFortran 77 wrappers are saved to \"/var/folders/bq/_946cdn92t7bqzz5frpfpw7r0000gp/T/tmp3y_jxtl_/src.macosx-10.5-x86_64-3.4/_fortran_magic_a044885f2b0c0feac78a230b6b714e2b-f2pywrappers.f\"\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 22
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"import numpy as np\n",
|
||
|
"\n",
|
||
|
"x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)])\n",
|
||
|
"y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)])"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"prompt_number": 23
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"fortran_lstsqr(x_ary, y_ary)"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"output_type": "pyout",
|
||
|
"prompt_number": 25,
|
||
|
"text": [
|
||
|
"(1.1313508052697814, 3.681685640167956)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 25
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<br>\n",
|
||
|
"<br>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "heading",
|
||
|
"level": 2,
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Running code from other interpreters: Ruby, Perl, and Bash"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"To use any interpreter that is installed on your system:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%script perl\n",
|
||
|
"print 'Hello, World!';"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Hello, World!"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 44
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Or use the magic command for the respective interpreter directly:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%perl\n",
|
||
|
"print 'Hello, World!';"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Hello, World!"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 45
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%ruby\n",
|
||
|
"puts \"Hello, World!\""
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Hello, World!\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 46
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%bash\n",
|
||
|
"echo \"Hello World!\""
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"Hello World!\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 47
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [
|
||
|
"%%script R --no-save\n",
|
||
|
"cat(\"Goodbye, World!\\n\")"
|
||
|
],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"stream": "stdout",
|
||
|
"text": [
|
||
|
"\n",
|
||
|
"R version 3.0.2 (2013-09-25) -- \"Frisbee Sailing\"\n",
|
||
|
"Copyright (C) 2013 The R Foundation for Statistical Computing\n",
|
||
|
"Platform: x86_64-apple-darwin10.8.0 (64-bit)\n",
|
||
|
"\n",
|
||
|
"R is free software and comes with ABSOLUTELY NO WARRANTY.\n",
|
||
|
"You are welcome to redistribute it under certain conditions.\n",
|
||
|
"Type 'license()' or 'licence()' for distribution details.\n",
|
||
|
"\n",
|
||
|
" Natural language support but running in an English locale\n",
|
||
|
"\n",
|
||
|
"R is a collaborative project with many contributors.\n",
|
||
|
"Type 'contributors()' for more information and\n",
|
||
|
"'citation()' on how to cite R or R packages in publications.\n",
|
||
|
"\n",
|
||
|
"Type 'demo()' for some demos, 'help()' for on-line help, or\n",
|
||
|
"'help.start()' for an HTML browser interface to help.\n",
|
||
|
"Type 'q()' to quit R.\n",
|
||
|
"\n",
|
||
|
"> cat(\"Goodbye, World!\\n\")\n",
|
||
|
"Goodbye, World!\n",
|
||
|
"> \n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"prompt_number": 55
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"collapsed": false,
|
||
|
"input": [],
|
||
|
"language": "python",
|
||
|
"metadata": {},
|
||
|
"outputs": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {}
|
||
|
}
|
||
|
]
|
||
|
}
|