{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy is the fundamental package for scientific computing with Python. It contains among other things:\n", "\n", "- a powerful N-dimensional array object\n", "- sophisticated (broadcasting) functions\n", "- tools for integrating C/C++ and Fortran code\n", "- useful linear algebra, Fourier transform, and random number capabilities\n", "\n", "Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.\n", "\n", "Library documentation: http://www.numpy.org/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from numpy import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# declare a vector using a list as the argument\n", "v = array([1,2,3,4])\n", "v" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# declare a matrix using a nested list as the argument\n", "M = array([[1,2],[3,4]])\n", "M" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, numpy.ndarray)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# still the same core type with different shapes\n", "type(v), type(M)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.size" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# arguments: start, stop, step\n", "x = arange(0, 10, 1)\n", "x" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.41666667, 0.83333333, 1.25 ,\n", " 1.66666667, 2.08333333, 2.5 , 2.91666667,\n", " 3.33333333, 3.75 , 4.16666667, 4.58333333,\n", " 5. , 5.41666667, 5.83333333, 6.25 ,\n", " 6.66666667, 7.08333333, 7.5 , 7.91666667,\n", " 8.33333333, 8.75 , 9.16666667, 9.58333333, 10. ])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linspace(0, 10, 25)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 3.03773178e+00, 9.22781435e+00,\n", " 2.80316249e+01, 8.51525577e+01, 2.58670631e+02,\n", " 7.85771994e+02, 2.38696456e+03, 7.25095809e+03,\n", " 2.20264658e+04])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "logspace(0, 10, 10, base=e)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0, 0, 0],\n", " [1, 1, 1, 1, 1],\n", " [2, 2, 2, 2, 2],\n", " [3, 3, 3, 3, 3],\n", " [4, 4, 4, 4, 4]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, y = mgrid[0:5, 0:5]\n", "x" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from numpy import random" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.88096372, 0.53238822, 0.17775764, 0.76591586, 0.6127709 ],\n", " [ 0.51258827, 0.05731522, 0.05610599, 0.36338405, 0.29548536],\n", " [ 0.54649788, 0.60544106, 0.38081415, 0.5717322 , 0.2426889 ],\n", " [ 0.96448533, 0.22105112, 0.41292727, 0.40652867, 0.57179488],\n", " [ 0.55815745, 0.22049273, 0.30680923, 0.82881023, 0.36665264]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.rand(5,5)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.40801047, -0.36738023, 0.0654462 , 0.16108406, 0.08391533],\n", " [-1.31495404, -1.31773965, 1.01225524, 0.28113264, -1.32523908],\n", " [ 1.09106398, -0.37571802, 2.01780085, 0.16072945, 1.0688331 ],\n", " [ 0.54306468, 0.9436181 , -2.60779314, 0.27348637, 0.60950091],\n", " [-1.0055051 , 1.77771874, 0.33209667, -0.10772336, -0.66501805]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# normal distribution\n", "random.randn(5,5)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 0],\n", " [0, 2, 0],\n", " [0, 0, 3]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diag([1,2,3])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.itemsize" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.nbytes" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.ndim" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 4)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[0], M[1,1]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([3, 4])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[1]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[7, 2],\n", " [3, 4]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# assign new value\n", "M[0,0] = 7\n", "M" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0],\n", " [3, 4]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[0,:] = 0\n", "M" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 3])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# slicing works just like with lists\n", "A = array([1,2,3,4,5])\n", "A[1:3]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array([[n+m*10 for n in range(5)] for m in range(5)])\n", "A" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row_indices = [1, 2, 3]\n", "A[row_indices]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 2])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# index masking\n", "B = array([n for n in range(5)])\n", "row_mask = array([True, False, True, False, False])\n", "B[row_mask]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear Algebra" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [], "source": [ "v1 = arange(0, 5)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 4, 5, 6])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 + 2" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 2, 4, 6, 8])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 * 2" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 4, 9, 16])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 * v1" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(v1, v1)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 30, 130, 230, 330, 430])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(A, v1)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 300, 310, 320, 330, 340],\n", " [1300, 1360, 1420, 1480, 1540],\n", " [2300, 2410, 2520, 2630, 2740],\n", " [3300, 3460, 3620, 3780, 3940],\n", " [4300, 4510, 4720, 4930, 5140]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# cast changes behavior of + - * etc. to use matrix algebra\n", "M = matrix(A)\n", "M * M" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# inner product\n", "v.T * v" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.+1.j, 0.+2.j],\n", " [ 0.+3.j, 0.+4.j]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C = matrix([[1j, 2j], [3j, 4j]])\n", "C" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.-1.j, 0.-2.j],\n", " [ 0.-3.j, 0.-4.j]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conjugate(C)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.+2.j , 0.-1.j ],\n", " [ 0.-1.5j, 0.+0.5j]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# inverse\n", "C.I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Statistics" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "23.0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean(A[:,3])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(14.142135623730951, 200.0)" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std(A[:,3]), var(A[:,3])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3, 43)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:,3].min(), A[:,3].max()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(45, 362880)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = arange(1, 10)\n", "sum(d), prod(d)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 3, 6, 10, 15, 21, 28, 36, 45])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cumsum(d)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 6, 24, 120, 720, 5040, 40320,\n", " 362880])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cumprod(d)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "110" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sum of diagonal\n", "trace(A)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.37938474, 0.93337301, 0.10864521],\n", " [ 0.144712 , 0.12270014, 0.622434 ],\n", " [ 0.16307745, 0.4850791 , 0.59703797]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = random.rand(3, 3)\n", "m" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0.93337300979654614, array([ 0.37938474, 0.93337301, 0.622434 ]))" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use axis parameter to specify how function behaves\n", "m.max(), m.max(axis=0)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# reshape without copying underlying data\n", "n, m = A.shape\n", "B = A.reshape((1,n*m))\n", "\n", "B" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44]])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# modify the array\n", "B[0,0:5] = 5\n", "B" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 5, 5, 5, 5],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# also changed\n", "A" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# creates a copy\n", "B = A.flatten()\n", "B" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(array([[1],\n", " [2],\n", " [3]]), (3L, 1L), (1L, 3L))" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# can insert a dimension in an array\n", "v = array([1,2,3])\n", "v[:, newaxis], v[:,newaxis].shape, v[newaxis,:].shape" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 2, 2, 2, 3, 3, 3])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repeat(v, 3)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 1, 2, 3, 1, 2, 3])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tile(v, 3)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [], "source": [ "w = array([5, 6])" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 5, 6])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "concatenate((v, w), axis=0)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# deep copy\n", "B = copy(A)" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 0 }