{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Numpy Tutorial" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "a --> [2 3 4 5]\n", "b --> [5 6 7 8]\n", "a+b --> [ 7 9 11 13]\n" ] } ], "source": [ "from numpy import *\n", "#from numpy import array\n", "import numpy as np\n", "a= array([2,3,4,5])\n", "b=array((5,6,7,8))\n", "print type(a)\n", "print \"a -->\", a\n", "print \"b -->\", b\n", "print \"a+b -->\", a+b\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This will give an error!!!\n", "a+c -->" ] }, { "ename": "ValueError", "evalue": "operands could not be broadcast together with shapes (4,) (6,) ", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"This will give an error!!!\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"a+c -->\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,) (6,) " ] } ], "source": [ "# You can only add arrays of the same shape / equal length:\n", "c=array([5,8,8,9,5,2])\n", "print \"This will give an error!!!\"\n", "print \"a+c -->\", a+c" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a+1 --> [3 4 5 6]\n" ] } ], "source": [ "# broadcasting\n", "# If you add an array to a scalar, the scalar gets broadcast across all the array elements\n", "print \"a+1 -->\", a+1\n", "# Now you can broadcast arrays and so you can add arrays of different shapes..." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Printing array x: [[ 1. 2. 3. 4.]\n", " [ 5. 6. 7. 8.]] \n", "\n", "\"Shape of array x is:\" (2, 4) \n", "\n", "\"Value at x[0][1] is:\" 2.0\n" ] } ], "source": [ "import numpy as np\n", "x= np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.float32)\n", "print \"Printing array x: \", x,\"\\n\"\n", "print \"\\\"Shape of array x is:\\\" \", x.shape,\"\\n\"\n", "print \"\\\"Value at x[0][1] is:\\\" \", x[0][1] # gives row0, c1 --> we start index from zero!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 1 -2]\n" ] } ], "source": [ "x=np.array([1, 3, 5, 6])\n", "y=np.array([1,2,3,1])\n", "d=y[1:]-y[:-1]\n", "print d\n", "# This runs in C, the loop happens in C, so it's fast.\n", "# It doesn't matter what shape y is. So, it can be a very big array." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14\n", "[ 2 5 9 14]\n" ] } ], "source": [ "print sum(a)\n", "# cumsum adds every emelement to the previous element\n", "print cumsum(a)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-------------------\n", "[0 1 2]\n", "[ 0. 1. 2.]\n", "-------------------\n", "[2 3 4 5 6]\n", "-------------------\n", "[2 4 6]\n", "-------------------\n", "[ 100. 215.443469 464.15888336 1000. ]\n" ] } ], "source": [ "import numpy as np\n", "#numpy.arange: http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html\n", "\"\"\"\n", "numpy.arange([start, ]stop, [step, ]dtype=None)\n", " Return evenly spaced values within a given interval.\n", " Values are generated within the half-open interval [start, stop) (in other words, the interval including\n", " start but excluding stop). For integer arguments the function is equivalent to the Python built-in range\n", " function, but returns an ndarray rather than a list.\n", "\"\"\"\n", "print \"-------------------\"\n", "print np.arange(3)\n", "print np.arange(3.0)\n", "print \"-------------------\"\n", "print np.arange(2,7)\n", "print \"-------------------\"\n", "print np.arange(2,7, 2)\n", "print \"-------------------\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " numpy.zeros\n", "-------------------\n", "[ 0. 0. 0. 0. 0.]\n", "-------------------\n", "[0 0 0 0 0 0 0 0 0 0]\n", "-------------------\n", "[[ 0.]\n", " [ 0.]\n", " [ 0.]]\n", "-------------------\n", "numpy.ones\n", "-------------------\n", "[ 1. 1. 1. 1. 1.]\n", "-------------------\n", "[ 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0]\n", "-------------------\n", "[[ 1.]\n", " [ 1.]\n", " [ 1.]]\n", "-------------------\n", "numpy.identity\n", "-------------------\n", "[[ 1. 0. 0. 0. 0.]\n", " [ 0. 1. 0. 0. 0.]\n", " [ 0. 0. 1. 0. 0.]\n", " [ 0. 0. 0. 1. 0.]\n", " [ 0. 0. 0. 0. 1.]]\n", "-------------------\n" ] } ], "source": [ "import numpy as np\n", "#------------------\n", "print \"numpy.zeros\"\n", "#------------------\n", "\"\"\"\n", " numpy.zeros(shape, dtype=float, order='C')¶\n", " Return a new array of given shape and type, filled with zeros.\n", " \n", "shape : int or sequence of ints\n", " Shape of the new array, e.g., (2, 3) or 2.\n", "\"\"\"\n", "print \"-------------------\"\n", "print np.zeros(5)\n", "print \"-------------------\"\n", "print np.zeros((10,), dtype=np.int)\n", "print \"-------------------\"\n", "print np.zeros((3, 1))\n", "print \"-------------------\"\n", "#------------------\n", "print \"numpy.ones\"\n", "#------------------\n", "\"\"\"\n", " numpy.ones(shape, dtype=None, order='C')\n", " Return a new array of given shape and type, filled with ones.\n", "\"\"\"\n", "print \"-------------------\"\n", "print np.ones(5)\n", "print \"-------------------\"\n", "print np.ones((10,), dtype=np.float128)\n", "print \"-------------------\"\n", "print np.ones((3, 1))\n", "print \"-------------------\"\n", "\n", "#------------------\n", "print \"numpy.identity\"\n", "#------------------\n", "\"\"\"\n", " numpy.identity(n, dtype=None)\n", " Return the identity array.\n", " The identity array is a square array with ones on the main diagonal.\n", "n : int\n", " Number of rows (and columns) in n x n output.\n", "\"\"\"\n", "print \"-------------------\"\n", "print np.identity(5)\n", "print \"-------------------\"\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " numpy.linspace\n", "[ 2. 2.25 2.5 2.75 3. ]\n", "-------------------\n", "[ 2. 2.2 2.4 2.6 2.8]\n", "-------------------\n", "(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)\n", "-------------------\n", "\n", "\n", " numpy.logspace\n", "---------------------------------------------------------\n", "[ 100. 215.443469 464.15888336 1000. ]\n", "---------------------------------------------------------\n", "[ 4. 5.0396842 6.34960421 8. ]\n", "---------------------------------------------------------\n", "[ 4. 4.75682846 5.65685425 6.72717132]\n", "---------------------------------------------------------\n" ] } ], "source": [ "import numpy as np\n", "#------------------\n", "print \"\\n numpy.linspace\"\n", "#------------------\n", "\"\"\"\n", " numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)[source]¶\n", " Return evenly spaced numbers over a specified interval.\n", " Returns num evenly spaced samples, calculated over the interval [start, stop].\n", " The endpoint of the interval can optionally be excluded.\n", " \n", "retstep : bool, optional\n", " If True, return (samples, step), where step is the spacing between samples.\n", "\n", "http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.linspace.html#numpy.linspace\n", "\"\"\"\n", "print np.linspace(2.0, 3.0, num=5)\n", "print \"-------------------\"\n", "print np.linspace(2.0, 3.0, num=5, endpoint=False)\n", "print \"-------------------\"\n", "print np.linspace(2.0, 3.0, num=5, retstep=True)\n", "print \"-------------------\\n\"\n", "#------------------\n", "print \"\\n numpy.logspace\"\n", "#------------------\n", "\"\"\"\n", " numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)\n", " Return numbers spaced evenly on a log scale.\n", " In linear space, the sequence starts at base ** start (base to the power of start) \n", " and ends with base ** stop (see endpoint below).\n", "\"\"\"\n", "print \"-------------------\"*3\n", "print np.logspace(2.0, 3.0, num=4)\n", "print \"-------------------\"*3\n", "print np.logspace(2.0, 3.0, base=2.0, num=4)\n", "print \"-------------------\"*3\n", "print np.logspace(2.0, 3.0, base=2.0, num=4, endpoint=False)\n", "print \"-------------------\"*3" ] } ], "metadata": { "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 }