{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "code_folding": [], "collapsed": false, "slideshow": { "slide_type": "skip" } }, "outputs": [ { "data": { "text/plain": [ "{u'start_slideshow_at': 'selected', u'theme': 'sky', u'transition': 'zoom'}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from notebook.services.config import ConfigManager\n", "from IPython.paths import locate_profile\n", "cm = ConfigManager(profile_dir=locate_profile(get_ipython().profile))\n", "cm.update('livereveal', {\n", " 'theme': 'sky',\n", " 'transition': 'zoom',\n", " 'start_slideshow_at': 'selected',\n", "})" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python crash course" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Installation\n", "\n", "- Download and install [Anaconda Python Distribution](https://store.continuum.io/cshop/anaconda/)\n", "- (Optional) Download [Academic license](https://store.continuum.io/cshop/academicanaconda) **.skolkovotech.ru** email is sufficient. It gives you access to the Intel MKL linear algebra package.\n", "- Run command in the terminal (if you have one, if you are on Windows, ask Evgeny Frolov what to do)\n", "\n", " ```bash\n", " jupyter notebook\n", " ``` \n", "(on Mac & Linux, on Windows should work somehow as well). \n", "You will get a new browser window with the list of your notebooks. If there none, you can create one!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Notebook structure\n", "\n", "- Notebook consists of **cells** of different types\n", "- Code in the cells is executed by pressing **Shift + Enter**\n", "- To add a cell, use **Insert** in the menu\n", "- To edit the cell, double-click on it.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Important points\n", "\n", "- Notebooks are good for short snipplets of the code\n", "- Big codes are typically written in a separate **.py** files and used as **packages**\n", "- It is a good idea to use Notebooks for the presentation of the final results of your computation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Importing packages\n", "\n", "You have to import several basic packages to start doing basic stuff for linear algebra and plotting. \n", "\n", "There are a lot of useful Python packages in the world for almost every task: always look on the web \n", "before writing your own code! \n", "\n", "Basic libraries:\n", "\n", "- **numpy** (for doing matrix & vector stuff)\n", "- **matplotlib** (for doing plotting), prettyplotlib (for nice plotting)\n", "- **scipy** (a huge math library, we will use it for sparse matrices) " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "#This how their import looks like\n", "%matplotlib inline \n", "#To show everything in a browser\n", "import numpy as np\n", "import matplotlib.pyplot as plt \n", "import scipy as sp" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**Notice the namespaces**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python vs MATLAB\n", "\n", "MATLAB is a well-known tool for doing numerical linear algebra computations, but it has many disadvantages. \n", "If you are an experienced MATLAB user, then you can look at [Numpy for MATLAB users](http://wiki.scipy.org/NumPy_for_Matlab_Users).\n", "\n", "- Indexing from 0\n", "- [ ] instead of ( ) for array element\n", "- Python is a powerful and flexible programming language :)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python syntax \n", "Best to learn is practice!\n", "\n", "- Use 4 whitespaces to denote a codeblock \n", " **Not like this!!**\n", "```\n", " for (i = 0, i < 5, i++){\n", " printf(\"Hi! \\n\");\n", "}\n", "```\n", " but like this:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi\n", "Hi3\n", "Hi\n", "Hi3\n", "Hi\n", "Hi3\n", "Hi\n", "Hi3\n", "Hi\n", "Hi3\n", "Hi2\n" ] } ], "source": [ "#Some print 'Hi' code\n", "for i in range(5):\n", " print('Hi')\n", " print('Hi3')\n", "print('Hi2')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import YouTubeVideo\n", "#Long-long tutorial\n", "YouTubeVideo('3Fp1zn5ao2M')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Arrays\n", "Numpy arrays is something that you need to know. \n", "\n", "\n", " It is typically a bad idea to write your own array processing code! " ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n", "[1, 2, 3, 4]\n" ] } ], "source": [ "#Create some numpy array and print it\n", "import numpy as np\n", "a = [1, 2, 3, 4]\n", "np_a = np.array(a)\n", "print np_a\n", "print a" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Variables\n", "Let us do some arithmetics demo" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0 4.0 1.0\n" ] } ], "source": [ "#Do some arithmetics\n", "b = 1.0\n", "a = 1\n", "c = a + b\n", "print c, c ** 2, a/b\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Loops\n", "Let us do some loops demo" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.0, 2.0, 3.0, 4.0, 3.0, 4.0, 5.0, 6.0]\n" ] } ], "source": [ "#Do some loop demo (i.e, create two lists and add them)\n", "a = [1.0, 2.0, 3.0, 4.0]\n", "b = [3.0, 4.0, 5.0, 6.0]\n", "c = [0.0, 0.0, 0.0, 0.0]\n", "for i in range(4):\n", " c[i] = a[i] + b[i]\n", "c = a + b\n", "print c" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Adding two lists...." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "#Now we can add two lists" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Adding two numpy arrays..." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 9. 32. 75. 144.]\n" ] } ], "source": [ "#Now we want to add two arrays\n", "print np.array(a) * np.array(b) ** 2" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-1.63638585 -0.41324712 -6.42705844]\n", "[[-1.26816793 -0.52981229 0.16159436]\n", " [-0. -1.05962458 0.64637746]\n", " [-3.80450379 -4.23849831 1.61594365]]\n" ] } ], "source": [ "#Do some matrix-by-vector operations \n", "\n", "a = np.array([[1.0, 0.5, 0.5], [0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])\n", "v = np.random.randn(3)\n", "print a.dot(v) #@\n", "print a * v" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Functions\n", "Let us create some functions!" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def mysum(a, b):\n", " c = np.zeros(len(a)) #All zeros\n", " for i in xrange(len(c)):\n", " c[i] = a[i] + b[i]\n", " return c" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "And add two large arrays!" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 35.1 ms per loop\n", "10000 loops, best of 3: 73.6 µs per loop\n" ] } ], "source": [ "M = 10 ** 5\n", "a = np.ones(M) #All ones\n", "%timeit c = mysum(a, a) #IPython magic function for timing\n", "%timeit c = a + a ** 2 #Numpy built-in function" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- **Why is the difference?**\n", "- Actually, there are many ways to solve the problem (Numpy, Cython, f2py, numba)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Numba\n", "One of the interesting tools is [Numba](http://numba.pydata.org/) \n", "Let us write some code..." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 1203.02 times longer than the fastest. This could mean that an intermediate result is being cached \n", "10000 loops, best of 3: 133 µs per loop\n", "1000 loops, best of 3: 1.01 ms per loop\n" ] } ], "source": [ "from numba import jit\n", "@jit(nopython=True)\n", "def mysum_nb(a, b):\n", " c = np.zeros(len(a))\n", " for i in range(len(a)):\n", " c[i] = a[i] + b[i] ** 3 + a[i] * b[i]\n", "\n", "\n", "M = 10 ** 5\n", "a = np.ones(M) #All ones\n", "%timeit c = mysum_nb(a, a) #IPython magic function for timing\n", "%timeit c = a + a ** 3 + a * a#Numpy built-in function" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Plotting\n", "To do plotting, you have to put in a preamble." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns #For prettier plots\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Some matrix stuff\n", "Our main object will be **matrix**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "from IPython.display import Image\n", "i = Image(filename='TheMatrix.jpg', retina = True)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Matrix-by-vector product \n", "$A$ is an $n \\times m$ matrix. $v$ is an $m \\times 1$ matrix (vector). \n", "$$\n", " y = A x,\n", "$$\n", "y is a vector of length $n$." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "#Multiply matrix by vector here" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Summary\n", "- Install & try to test Anaconda Python distribution\n", "- Bother TAs with questions\n", "- First homework to be delivered end of this week.\n", "\n", "Stellar is still here." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "[Lecture 0.5](lec-1.ipynb)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "skip" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"./styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }