{ "metadata": { "name": "", "signature": "sha256:826956760a3a35c02ef0fcc81f30e0a7110e214c6cf9dd1b0bec7e0fb67c943d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "for help with matkdown in ipython notebooks, refer to this [post](https://help.github.com/articles/github-flavored-markdown/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where are we\n", "===" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "you are currently viewing an ipython notebook file online. If you look at the url closely, it will show you that you are currently looking at a .ipynb file, located in a github repository online, viewed using the nb(notebook) viewer. Or, you are looking at a .html file, maybe even offline. For those of you familiar with mathematica, the ipython notebook is similar to a mathematica notebook. For the rest, an ipython notebook is a medium in which one can run python code, write elaborate appendices (in LaTeX) explaining the theory behind code and include plots or figures relevant to the code. Simply put, IPython and the IPython notebook are python working environments. At it is my choice of working working environment because, as mentioned earlier, this notebook can be converted into a .html file, making it easier for me to share the notebook online, very much unlike mathematica notebooks for those of you with experience.\n", "\n", "Working in python and working environments in python are not necessarily the same. If you want to learn or use python, the noob way to do it would be to work on the python prompt, which can be accessed on a unix terminal by typing python. This is how I started learning python, not exactly an easy way to start working in python. Granted, this has taught me a lot of things about python and a few interesting tricks but again, it's easy for someone to get put off by having to work on the python prompt." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", ">>>\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An easier way to work in python is by executing python files on the terminal. For those of you comfortable with pre-compiled languages like C or C++, you will notice that python files don't need to be compiled before being executed. Python is a dynamically-typed language, unlike statically-typed languages like C or C++." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "$ python foo_bar.py\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "you could've also executed files on the python prompt using" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", ">>> execfile('foo_bar.py')\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "now, coming to working environments, for those of you who have worked on MATLAB before, you'll understand that MATLAB comes with a working environment i.e it installs all of the necessary libraries, it has GUI elements that show the data loaded during execution, it displays the plots using other GUI elements and it has an easy way to search, access and install other packages. Similarly, examples of working environments for python are [IPython](ipython.org/), [anaconda](https://store.continuum.io/cshop/anaconda), [canopy](https://www.enthought.com/products/canopy/), [PYcharm](www.jetbrains.com/pycharm/) and so on and so forth. I have only barely scratched anaconda and canopy and I mostly work on IPython, working on the terminal a few times.\n", "\n", "Vaguely speaking, a working environments makes life easier to work using a specific language. Visualising data accessed by the program is hard while working on the python prompt or the terminal but easy on the working environments. Accessing and installing packages will be a bit of a hassle normally but working environments come with GUI elements that make life easy.\n", "\n", "As I mentioned, I mostly use IPython to code in python, specifically the IPython notebook. IPython comes with certain packages pre-installed but the reason I use it the most is because of the IPython notebook, referred to herewith as ipynb. ipynb files have text cells, like this one, and code cells, like what you will see further. The python interpreter executes the code in code cells and the text cells, as I mentioned earlier, help with documentation and comments. One can also include figures or plots in an ipynb file, as you will see further. Overall, using ipynb files helps me find my code, my comments/documentation and my results at the same place.\n", "\n", "So, that's where we are. On an ipython notebook file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython doesn't come with a python package manager and beginners often get confused as to how to install python packages and there are no executable .exe files in the library/package. It is easier to use a package manager and I use [pip](https://pypi.python.org/pypi/pip) for this. I urge you to read further about it on your own.\n", "\n", "And if you want to go further, you can setup a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/) to work in python, where different virtual environments have different packages installed. It might not be necessary for the beginner to have various versions of the same package installed for the various projects he/she's working on but when that day comes, virtual environments is how to solve that problem." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enough talk, let's code.\n", "===" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, assuming that you are comfortable with C, I shall try compare C and python code that perform the same action." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* how to load data, from user input to loading data from files\n", "* how to operate on data\n", "* how to print data, onto the terminal or a visual medium or write to files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'Hello World'\n", "print '\\n'\n", "# if you want to print to multiple lines,\n", "print 'Hello', '\\n', 'World'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World\n", "\n", "\n", "Hello \n", "World\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "the equivalant in C would be" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "#include \n", "void main()\n", "{\n", " printf ('Hello World');\n", "}\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "print 'the value of a is', a\n", "print '\\n'\n", "# if you want to specify the\n", "# data type to be used\n", "#print 'the value of a is %(f)', a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "the value of a is 10\n", "\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "the equivalant in C would be " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "#include \n", "void main()\n", "{\n", " int a;\n", " a = 10;\n", " printf ('the value of a is %d', %a);\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "you will notice that you didn't have to define the data type of python explicitly, as you did in C. This is because the python interpreter will automatically assign the data type depending on the value which you feed it. notice the difference that a . will make" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'the value of 1/2 is', 1/2\n", "print 'n'\n", "print 'the value of 1/2 is', 1./2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "the value of 1/2 is 0\n", "n\n", "the value of 1/2 is 0.5\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "you will also note that we didn't need to include any libraries, like how we include stdio.h in C, to run python programs. the python interpreter takes care of most of the trivial things like defining data types, printing, reading and writing to files, iterating over a variable, arrays or lists and so on. But when it is time to include/refer to a library, it can be done as such" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "where numpy is a library and I am referring to it as np. Libraries in python have a heirarchy, similar to library.module.submodule.function. And inorder to access the function, one has to call\n", "library.module.submodule.function(args)\n", "on the other hand, we can choose to import the submodule specifically instead of the whole library using\n", "import library.module.submodule as sb\n", "and then call the function as\n", "sb.function(args).\n", "taking an example," ] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "plt.plot()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "[]" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "[matplotlib](matplotlib.org/) is one of the more commonly used plotting libraries in python. other do exist, such as [ggplot](http://blog.yhathq.com/posts/ggplot-for-python.html), [bokeh](bokeh.pydata.org), [d3](http://d3js.org) and so on but I use matplotlib for almost all of my plotting needs in python. it is also interesting to note that ipython and matplotlib work together i.e plots constructed using matplotlib can be inserted into the ipython notebook using\n", "\n", "```\n", "%matplotlib inline\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "it is important to note that the \n", "```\n", "$ ipython notebook\n", "```\n", "command needs to be run from your working directory i.e the directory where all of your data exists, data which you would like to load, manipulate and make sense of. if it so happens that you are running ipython notebook from a directory different from that which contains your data, not to worry. the OS library in python comes to the rescue.\n", "\n", "linux commands, that i hope you are familiar with, such as pwd, ls and so on can be accessed from the ipython notebook by adding the '!' prefix to the command. for example, one can look up the current working directory by typing in \n", "```\n", "!pwd\n", "```\n", "and inorder to change the working directory, you will have to\n", "```\n", "import os\n", "os.chdir('dir_containing_data')\n", "```\n", "one could also use\n", "```\n", "os.getcwd()\n", "```\n", "to get the current working directory but it is to be noted that\n", "```\n", "!cd dir_containing_data\n", "```\n", "will not work." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you do not want to be bothered with the python working environment and wish to just start directly on the python prompt, be my guest. You can skip the first few sections on virtual environments and jump to hello world.\n", "\n", "- python lists and operations on list like sum of elements, mean.\n", "- introduction to loops.\n", "- introduce to numpy arrays\n", "\n", "- interesting libraries\n", "- numpy array utilities, convenience functions and numpy operations similar to those done above\n", "- array to list and list to array conversion\n", "- loading data from a file and saving data to a file\n", "- plotting and matplotlib utilities\n", "- over plotting and subplots\n", "\n", "- timeit in python\n", "- references" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">Know what version you are working with" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import numpy\n", "import scipy\n", "#import numpy as np\n", "import matplotlib\n", "#import matplotlib.pyplot as plt\n", "import IPython\n", "# similar to\n", "# #include in c\n", "print 'the version of python we are using is',(sys.version)\n", "print 'the version of numpy is ', numpy.version.version, '\\n',\\\n", " 'the version of scipy is', scipy.version.version, '\\n',\\\n", " 'the version of matplotlib is ', matplotlib.__version__\n", "print 'the version of IPython being used is', IPython.__version__" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "the version of python we are using is 2.7.6 (default, Mar 22 2014, 22:59:38) \n", "[GCC 4.8.2]\n", "the version of numpy is 1.8.2 \n", "the version of scipy is 0.13.3 \n", "the version of matplotlib is 1.4.0\n", "the version of IPython being used is 1.2.1\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ ">library\n", ">>module\n", ">>>submodule\n", ">>>>function\n", "\n", "for example, if we intend to use the plotting routine in matplotlib library, we will need to look for it under\n", "\n", ">matplotlib\n", ">>pyplot\n", ">>>plot\n", "\n", "similarly, if we needed to access the linear equation solver from the scipy library, we will need to look for it under\n", "\n", ">scipy\n", ">>linalg\n", ">>>linalg_solver" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "in comparison, functions in C don't have a nested structure. if we define a special function newFunction in a file newFunctionsFile.c, we include\n", "```\n", "#include 'newFunctionfFile.c'\n", "```\n", "after we include the appropriate headers and we can call newFunction directly.\n", "\n", "if one tries emulating the same thing in python, by defining a newFunction in a file newFunctionsFile.py, one needs to include\n", "```\n", "import newFunctionsFile\n", "```\n", "and then call the newFunction using\n", "```\n", "newFunctionsFile.newFunction(*args)\n", "```\n", "or like we have done earlier, use a short hand for newFunctionsFile as follows\n", "```\n", "import newFunctionsFile as src\n", "```\n", "and then call the newFunction using\n", "```\n", "src.newFunction(*args)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "libraries have the structure\n", "```\n", "library.module.submodule.function\n", "```\n", "the function plot is in the matplotlib library's pyplot module.\n", "```\n", "matplotlib.pyplot.plot('args')\n", "```\n", "is how we have to normally call it but instead, by doing\n", "```\n", "import matplotlib.pyplot as plt\n", "```\n", "we can call it using\n", "```\n", "plt.plot('args')\n", "```\n", "for convenience." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "printing\n", "====" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'hello world'\n", "# it's that simple" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hello world\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "var = 4\n", "print 'var =', var\n", "# inorder to print var" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "var = 4\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "> mention how to format output to a specific length or to a specific precision!!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "loops in python\n", "===" ] }, { "cell_type": "code", "collapsed": false, "input": [ "temp = 0\n", "for i in range(10):\n", " temp += 1\n", "print 'sum of first ten integers = ', temp\n", "# note that in python, the loop starts from 0 and ends at n-1\n", "# in the above mentioned case, the loop goes from 0 to 9!" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "sum of first ten integers = 10\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "# operations involving arrays can make use of len(array)\n", "array = [1,2,3,4]\n", "for i in xrange(len(array)):\n", " print array[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "plotting\n", "===" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "# as mentioned above,\n", "# library.module.submodule.fuction(arg1, arg2)\n", "# is the way in which we can access the various functions in python libraries\n", "# in this case, the plotting feature is in matplotlib.pyplot\n", "# and instead of having to write matplotlib.pyplot everytime\n", "# we refer to it using plt instead\n", "%matplotlib inline\n", "# is why i love ipython-notebooks\n", "# we can embed plots between the code!#" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(array,'o')\n", "# plt.show()\n", "# if you're not working on ipython,\n", "# you will have to run plt.show()\n", "# to display the plot" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "[]" ] }, { "metadata": {}, "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEACAYAAABI5zaHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEpRJREFUeJzt3V9oVHfex/HPsSOGMRKbCyMkgd3V7G5G68ykC3NjzQTR\nPElqCFRKC7WhtTBYNql76fosjTypsOxFMcpKeiNIYV3wZsMm2nrh1NpsGtyNSyEXxtDgTHQDQdLd\nLMb8mfNcPE9nOyZm/mSSOF/fLwhk5vw88zv82renv3iq47quKwCAKRvWewIAgPwj7gBgEHEHAIOI\nOwAYRNwBwCDiDgAGZRT3hYUFBYNBHTp0aMnj7e3tqqqqkt/v19DQUF4nCADIXkZxP3PmjHw+nxz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yfY7jqL+/X36/X42NjRoeHl7raa6aQl67dCyt29jYmIaGhhQKhVLet7J+T7u+Ql/DRCKhQCCg\nsrIy1dXVyefzpRzPdv1W/Echf2i5h5d+6MnfXTP9destk3nW1NQoFovJ6/XqypUramlp0Z07d9Zg\ndmujUNcuHSvrNj09rcOHD+vMmTMqLi5edLzQ12+56yv0NdywYYNu376t7777TvX19YpGowqHwylj\nslm/vN65l5eXKxaLJV/HYjFVVFQsOyYej6u8vDyf01g1mVzfli1bkv951dDQoLm5OT18+HBN57la\nCnnt0rGwbnNzc3rttdf01ltvqaWlZdHxQl+/dNdnYQ0lqaSkRE1NTbp161bK+9muX17j/otf/EIj\nIyMaGxvT7Oys/vjHP6q5uTllTHNzsy5evChJyz709CzK5PomJiaSv7sODg7Kdd1Fe2eFqpDXLp1C\nXzfXdXX06FH5fD4dP358yTGFvH6ZXF8hr+Hk5KSmpqYkSY8ePdK1a9cUDAZTxmS7fnndlvF4PDp3\n7pzq6+u1sLCgo0ePqrq6Wt3d3ZIK/6GnTK7v8uXLOn/+vDwej7xery5durTOs86c5QfW0l1bIa+b\nJH311Vf69NNPtWfPnmQUTp8+rXv37kkq/PXL5PoKeQ0fPHig1tZWJRIJJRIJHTlyRPv3719RO3mI\nCQAM4q/ZAwCDiDsAGETcAcAg4g4ABhF3ADCIuAOAQcQdAAwi7gBg0P8CfxMpFJRLxgkAAAAASUVO\nRK5CYII=\n", "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "> instead of this boring plot, introduce the random library and functions in python to generate and then plot a random sample. also show examples of scatter plots and histograms." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "coming to arrays, one has two options. a python list or a numpy array. a python list and a numpy array differ on a fundamental level at which they handle data. a numpy array stores data in sequential memory addressess, similar to C, where as a python list doesn't. This difference leads to a difference in time that it takes to operate on large arrays. It is suggested that one use numpy arrays instead of python lists for this specific reason.\n", "\n", "Although, if for whatever reason you are restricted to use python lists, you can convert them to numpy arrays using\n", "\n", "```\n", "numpy.asarray(foolist)\n", "```\n", "\n", "One should also note that most standard libraries work with numpy arrays and will not accept python lists as an input/argument. There's another reason to stick to numpy arrays from the beginning. For reasons that I cannot fathom, if you need to convert a numpy array to a list, you can do so using \n", "\n", "```\n", "fooarray.tolist()\n", "```\n", "\n", "maybe you have a gun to your head and are being made to convert numpy's beautiful arrays into python's crude but effective lists. I'm not judging you.\n", "\n", "There are numerous convenience functions in numpy to do perform array operations. You can refer to the examples [here](http://docs.scipy.org/doc/numpy/reference/).\n", "\n", "If you are trying to append an existing array, a commonly used operation,\n", "\n", "```\n", "foolist.append(barvalue)\n", "```\n", "\n", "or in the case of numpy arrays\n", "\n", "```\n", "numpy.append(fooarray,barvalue)\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print numpy.array(['foo','bar',1,2,3]), ['foo','bar',1,2,3]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['foo' 'bar' '1' '2' '3'] ['foo', 'bar', 1, 2, 3]\n" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "and finally, coming to sharing your work, you can either use [nbviewer](nbviewer.ipython.org) or you can convert the .ipynb file into a .html or a .py file using the command\n", "\n", "```\n", "$ ipython nbconvert --to html foobar.ipynb\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }