See More

{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Numpy Tutorial" ] }, { "cell_type": "code", "execution_count": 64, "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": [ "# Import numpy, conventionally as \"np\"\n", "import numpy as np\n", "# Numpy enables creation of N-dimensional arrays of data, or ndarrays\n", "a=np.array([2,3,4,5])\n", "b=np.array((5,6,7,8))\n", "print type(a)\n", "print \"a -->\", a\n", "print \"b -->\", b\n", "print \"a+b -->\", a+b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4,)\n" ] } ], "source": [ "# We can get the shape of the array, which is a tuple of the sizes of its dimensions\n", "print(a.shape)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" ] } ], "source": [ "z=np.zeros(10)\n", "print(z)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(10,)\n" ] } ], "source": [ "print(z.shape)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(z.ndim)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]\n" ] } ], "source": [ "# If we had an 2*5 ndarray, and we can intialize with \"zeros\" or \"ones\":\n", "x=np.zeros([2, 5])\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 5)\n", "2\n" ] } ], "source": [ "print(x.shape)\n", "print(x.ndim)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n" ] } ], "source": [ "# Or we can initialize with a shape of 4, 9:\n", "x=np.zeros([4, 9])\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 9)\n", "2\n" ] } ], "source": [ "print(x.shape)\n", "print(x.ndim)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[ 1. 1. 1.]\n", " [ 1. 1. 1.]\n", " [ 1. 1. 1.]\n", " [ 1. 1. 1.]]\n", "\n", " [[ 1. 1. 1.]\n", " [ 1. 1. 1.]\n", " [ 1. 1. 1.]\n", " [ 1. 1. 1.]]]\n" ] } ], "source": [ "# We can also create an array of > 2 dimensions\n", "# Consider the following from the documentation of scipy: https://docs.scipy.org/doc/numpy-dev/user/quickstart.html:\n", "\"\"\"\n", "When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:\n", "\n", " the last axis is printed from left to right,\n", " the second-to-last is printed from top to bottom,\n", " the rest are also printed from top to bottom, with each slice separated from the next by an empty line.\n", "\n", "One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.\n", "\"\"\"\n", "x=np.ones([2, 4, 3])\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 4, 3)\n", "3\n" ] } ], "source": [ "print(x.shape)\n", "print(x.ndim)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]\n", "\n", " [[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]\n", "\n", " [[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]]\n", "\n", "\n", " [[[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]\n", "\n", " [[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]\n", "\n", " [[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]]]\n" ] } ], "source": [ "x=np.zeros([2, 3, 4, 5])\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 3, 4, 5)\n", "4\n" ] } ], "source": [ "print(x.shape)\n", "print(x.ndim)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "float64\n" ] } ], "source": [ "print(x.dtype)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[1 1 1]\n", " [1 1 1]\n", " [1 1 1]\n", " [1 1 1]]\n", "\n", " [[1 1 1]\n", " [1 1 1]\n", " [1 1 1]\n", " [1 1 1]]]\n", "int32\n" ] } ], "source": [ "# Note array data type...\n", "x=np.ones([2, 4, 3], dtype=np.int32)\n", "print(x)\n", "print(x.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operations on arrays:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[5 5 5]\n", " [5 5 5]\n", " [5 5 5]\n", " [5 5 5]]\n", "\n", " [[5 5 5]\n", " [5 5 5]\n", " [5 5 5]\n", " [5 5 5]]]\n" ] } ], "source": [ "print(x*5)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[ 0.2 0.2 0.2]\n", " [ 0.2 0.2 0.2]\n", " [ 0.2 0.2 0.2]\n", " [ 0.2 0.2 0.2]]\n", "\n", " [[ 0.2 0.2 0.2]\n", " [ 0.2 0.2 0.2]\n", " [ 0.2 0.2 0.2]\n", " [ 0.2 0.2 0.2]]]\n" ] } ], "source": [ "print(x/5.0)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[1 1 1]\n", " [1 1 1]\n", " [1 1 1]\n", " [1 1 1]]]\n" ] } ], "source": [ "# We can slice\n", "my_slice=x[1:2]\n", "print(my_slice)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# You can only add arrays of the same shape / equal length:\n", "c=np.array([5,8,8,9,5,2])\n", "# print \"This will give an error if you print it!!!\"\n", "# print \"a+c -->\", a+c" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a --> [2 3 4 5]\n", "a+1 --> [4 5 6 7]\n" ] } ], "source": [ "# broadcasting\n", "# If you add an array to a scalar, the scalar gets broadcast across all the array elements\n", "print \"a -->\", a\n", "print \"a+1 -->\", a+2\n", "# Now you can broadcast arrays and so you can add arrays of different shapes..." ] }, { "cell_type": "code", "execution_count": 52, "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": [ "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": "markdown", "metadata": {}, "source": [ "## More operations" ] }, { "cell_type": "code", "execution_count": 53, "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": 55, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 6 15 6]\n" ] } ], "source": [ "print(x*y)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# You cannot do the below:\n", "# You will get an error:\n", "# ValueError: operands could not be broadcast together with shapes (4,) (5,)\n", "x=np.array([1, 3, 5, 6])\n", "y=np.array([1,2,3,1, 9])\n", "d=y[1:]-y[:-1]\n", "print(x*y)" ] }, { "cell_type": "code", "execution_count": 59, "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 np.cumsum(a)" ] }, { "cell_type": "code", "execution_count": 61, "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 7 12 17 22 27 32 37 42 47]\n", "-------------------\n" ] } ], "source": [ "#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,50, 5)\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": [ "#------------------\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": [ "#------------------\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.12" } }, "nbformat": 4, "nbformat_minor": 0 }