"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"
"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",
"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)."
"\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",