{"worksheets": [{"cells": [{"source": ["Let's import the `numpy` module."], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 1, "input": ["import numpy as np"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [], "metadata": {}}, {"prompt_number": 3, "input": ["n = 10 # CHANGE ME\n", "a1 = list(range(n))\n", "a2 = np.arange(n)\n", "\n", "if n <= 10:\n", " print(a1)\n", " print(a2)"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"stream": "stdout", "output_type": "stream", "text": ["[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[0 1 2 3 4 5 6 7 8 9]\n"]}], "metadata": {}}, {"prompt_number": 4, "input": ["%timeit [i**2 for i in a1]"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"stream": "stdout", "output_type": "stream", "text": ["100000 loops, best of 3: 2.36 \u00b5s per loop\n"]}], "metadata": {}}, {"prompt_number": 5, "input": ["%timeit a2**2"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"stream": "stdout", "output_type": "stream", "text": ["1000000 loops, best of 3: 1.06 \u00b5s per loop\n"]}], "metadata": {}}, {"source": ["Numpy Arrays: much less flexible, but:\n", "\n", "* much faster\n", "* less memory"], "metadata": {}, "cell_type": "markdown"}, {"source": ["---\n", "\n", "Ways to create a numpy array:\n", "\n", "* Casting from a list"], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 6, "input": ["np.array([1,2,3])"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["array([1, 2, 3])"], "prompt_number": 6, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"source": ["* `linspace`"], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 7, "input": ["np.linspace(-1, 1, 10)"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["array([-1. , -0.77777778, -0.55555556, -0.33333333, -0.11111111,\n", " 0.11111111, 0.33333333, 0.55555556, 0.77777778, 1. ])"], "prompt_number": 7, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"source": ["* `zeros`"], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 8, "input": ["np.zeros((10,10), np.float64)"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"], "prompt_number": 8, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"source": ["---\n", "\n", "Operations on arrays propagate to all elements:"], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 9, "input": ["\n", "a = np.array([1.2, 3, 4])\n", "b = np.array([0.5, 0, 1])"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [], "metadata": {}}, {"source": ["Addition, multiplication, power, .. are all elementwise:"], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 10, "input": ["a+b"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["array([ 1.7, 3. , 5. ])"], "prompt_number": 10, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"prompt_number": 11, "input": ["a*b"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["array([ 0.6, 0. , 4. ])"], "prompt_number": 11, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"prompt_number": 12, "input": ["a**b"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["array([ 1.09544512, 1. , 4. ])"], "prompt_number": 12, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"source": ["Matrix multiplication is `np.dot(A, B)` for two 2D arrays."], "metadata": {}, "cell_type": "markdown"}, {"source": ["---\n", "\n", "Numpy arrays have two (most) important attributes:"], "metadata": {}, "cell_type": "markdown"}, {"prompt_number": 13, "input": ["a = np.random.rand(5, 4, 3)\n", "a.shape"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["(5, 4, 3)"], "prompt_number": 13, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"prompt_number": 14, "input": ["a.dtype"], "cell_type": "code", "language": "python", "collapsed": false, "outputs": [{"text": ["dtype('float64')"], "prompt_number": 14, "output_type": "pyout", "metadata": {}}], "metadata": {}}, {"source": ["Other `dtype`s include `np.complex64`, `np.int32`, ..."], "metadata": {}, "cell_type": "markdown"}], "metadata": {}}], "nbformat_minor": 0, "nbformat": 3, "metadata": {"name": "", "signature": "sha256:9faa9b10f4f4cf3bb4bb9a0a0013625a8dec13a1e23555a6564450db8171565d"}}