{
 "metadata": {
  "name": "",
  "signature": "sha256:973068831c3b7e31b3f17ec644c85ffbdc9732992600267dfe44a68ac14b511e"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#Numpy"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Numpy is a fundamental package for scientific computing in Python. It contains:\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"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Create numpy arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "np.array(range(5))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 1,
       "text": [
        "array([0, 1, 2, 3, 4])"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.zeros(5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "array([ 0.,  0.,  0.,  0.,  0.])"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.ones([5,2])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "array([[ 1.,  1.],\n",
        "       [ 1.,  1.],\n",
        "       [ 1.,  1.],\n",
        "       [ 1.,  1.],\n",
        "       [ 1.,  1.]])"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.ndarray((3,4,5,5));\n",
      "arr.ndim, arr.dtype"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "(4, dtype('float64'))"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "numbers = np.ones(300, dtype='int32')\n",
      "numbers"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
        "       1], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = numbers.reshape(3,4,5,5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "(3, 4, 5, 5)"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Accessing numpy arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0][0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "array([[1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0][0][0] = range(5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0][0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "array([[0, 1, 2, 3, 4],\n",
        "       [1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1],\n",
        "       [1, 1, 1, 1, 1]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0][0] = [range(5)] * 5\n",
      "arr[0][0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "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]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "numpy notation to slice"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0,0,:,:]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 96,
       "text": [
        "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]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 96
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0,0,:,2:4]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 97,
       "text": [
        "array([[2, 3],\n",
        "       [2, 3],\n",
        "       [2, 3],\n",
        "       [2, 3],\n",
        "       [2, 3]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 97
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0,0:2,:,2:4]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 98,
       "text": [
        "array([[[2, 3],\n",
        "        [2, 3],\n",
        "        [2, 3],\n",
        "        [2, 3],\n",
        "        [2, 3]],\n",
        "\n",
        "       [[1, 1],\n",
        "        [1, 1],\n",
        "        [1, 1],\n",
        "        [1, 1],\n",
        "        [1, 1]]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 98
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "3 dots represents as many column as needed to complete an indexing tuple"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0,0,...]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 101,
       "text": [
        "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]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 101
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0,...,0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 104,
       "text": [
        "array([[ 0,  5, 10, 15, 20],\n",
        "       [ 5,  5,  5,  5,  5],\n",
        "       [ 5,  5,  5,  5,  5],\n",
        "       [ 5,  5,  5,  5,  5]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 104
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Simple operations"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0,0,:,:] += np.transpose(arr[0,0,:,:])\n",
      "arr[0,0,...]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 102,
       "text": [
        "array([[0, 1, 2, 3, 4],\n",
        "       [1, 2, 3, 4, 5],\n",
        "       [2, 3, 4, 5, 6],\n",
        "       [3, 4, 5, 6, 7],\n",
        "       [4, 5, 6, 7, 8]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 102
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr *= 5\n",
      "arr[0,0,...]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 103,
       "text": [
        "array([[ 0,  5, 10, 15, 20],\n",
        "       [ 5, 10, 15, 20, 25],\n",
        "       [10, 15, 20, 25, 30],\n",
        "       [15, 20, 25, 30, 35],\n",
        "       [20, 25, 30, 35, 40]], dtype=int32)"
       ]
      }
     ],
     "prompt_number": 103
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Stack"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = np.arange(5)\n",
      "a, a.shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 115,
       "text": [
        "(array([0, 1, 2, 3, 4]), (5,))"
       ]
      }
     ],
     "prompt_number": 115
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = np.vstack([a, np.arange(5)])\n",
      "a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 112,
       "text": [
        "array([[0, 1, 2, 3, 4],\n",
        "       [0, 1, 2, 3, 4]])"
       ]
      }
     ],
     "prompt_number": 112
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = np.vstack([a,(7,8,9,10,11)])\n",
      "a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 113,
       "text": [
        "array([[ 0,  1,  2,  3,  4],\n",
        "       [ 0,  1,  2,  3,  4],\n",
        "       [ 7,  8,  9, 10, 11]])"
       ]
      }
     ],
     "prompt_number": 113
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a.shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 114,
       "text": [
        "(3, 5)"
       ]
      }
     ],
     "prompt_number": 114
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}