Created
June 1, 2012 00:50
-
-
Save jiffyclub/2847673 to your computer and use it in GitHub Desktop.
NumPy IPython Notebook - 31 May, 2012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "NumpyLesson" | |
}, | |
"nbformat": 2, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# NumPy Lesson - May 31, 2012 #" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"NumPy Docs: http://docs.scipy.org/doc/numpy/reference/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"import numpy as np" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 1. Building Arrays ##", | |
"", | |
"### From Other Sequences ###", | |
"dtypes: http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"a = np.array([1, 2, 3, 10])" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 1 2 3 10]" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b = np.array([[1, 2, 3, 4], [5, 6, 7, 10.0]], dtype=np.float32)" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": [ | |
"array([[ 1., 2., 3., 4.],", | |
" [ 5., 6., 7., 10.]], dtype=float32)" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b.dtype" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 17, | |
"text": [ | |
"dtype('float32')" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### NumPy Generation Functions ###", | |
"http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"z = np.zeros((3, 3),dtype=np.int32)", | |
"print z" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[0 0 0]", | |
" [0 0 0]", | |
" [0 0 0]]" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"o = np.ones((3, 3))", | |
"print o" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 1. 1. 1.]", | |
" [ 1. 1. 1.]", | |
" [ 1. 1. 1.]]" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = np.arange(1, 2, 0.01)", | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 1. 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.1 1.11", | |
" 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.2 1.21 1.22 1.23", | |
" 1.24 1.25 1.26 1.27 1.28 1.29 1.3 1.31 1.32 1.33 1.34 1.35", | |
" 1.36 1.37 1.38 1.39 1.4 1.41 1.42 1.43 1.44 1.45 1.46 1.47", | |
" 1.48 1.49 1.5 1.51 1.52 1.53 1.54 1.55 1.56 1.57 1.58 1.59", | |
" 1.6 1.61 1.62 1.63 1.64 1.65 1.66 1.67 1.68 1.69 1.7 1.71", | |
" 1.72 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.8 1.81 1.82 1.83", | |
" 1.84 1.85 1.86 1.87 1.88 1.89 1.9 1.91 1.92 1.93 1.94 1.95", | |
" 1.96 1.97 1.98 1.99]" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.linspace(1, 2, num=50, endpoint=False)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 25, | |
"text": [ | |
"array([ 1. , 1.02, 1.04, 1.06, 1.08, 1.1 , 1.12, 1.14, 1.16,", | |
" 1.18, 1.2 , 1.22, 1.24, 1.26, 1.28, 1.3 , 1.32, 1.34,", | |
" 1.36, 1.38, 1.4 , 1.42, 1.44, 1.46, 1.48, 1.5 , 1.52,", | |
" 1.54, 1.56, 1.58, 1.6 , 1.62, 1.64, 1.66, 1.68, 1.7 ,", | |
" 1.72, 1.74, 1.76, 1.78, 1.8 , 1.82, 1.84, 1.86, 1.88,", | |
" 1.9 , 1.92, 1.94, 1.96, 1.98])" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 2. Indexing Arrays ##", | |
"http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"a = np.arange(10)" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 27, | |
"text": [ | |
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a[5:8]" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 29, | |
"text": [ | |
"array([5, 6, 7])" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = np.arange(10).reshape((2, 5))", | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[0 1 2 3 4]", | |
" [5 6 7 8 9]]" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a[1, :]" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
"array([5, 6, 7, 8, 9])" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a[(a < 3) & (a > 1)]" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 36, | |
"text": [ | |
"array([2])" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b = np.arange(10, 20).reshape((2, 5))", | |
"print b" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[10 11 12 13 14]", | |
" [15 16 17 18 19]]" | |
] | |
} | |
], | |
"prompt_number": 38 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b[a > 5]" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 40, | |
"text": [ | |
"array([16, 17, 18, 19])" | |
] | |
} | |
], | |
"prompt_number": 40 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a > 5" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 41, | |
"text": [ | |
"array([[False, False, False, False, False],", | |
" [False, True, True, True, True]], dtype=bool)" | |
] | |
} | |
], | |
"prompt_number": 41 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a[a > 5] *= 2", | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 0 1 2 3 4]", | |
" [ 5 12 14 16 18]]" | |
] | |
} | |
], | |
"prompt_number": 42 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.where(a > 5)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 43, | |
"text": [ | |
"(array([1, 1, 1, 1]), array([1, 2, 3, 4]))" | |
] | |
} | |
], | |
"prompt_number": 43 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b[np.where(a > 5)]" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 44, | |
"text": [ | |
"array([16, 17, 18, 19])" | |
] | |
} | |
], | |
"prompt_number": 44 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 3. Array Math ##" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = np.arange(10)", | |
"a = a * 2", | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0 2 4 6 8 10 12 14 16 18]" | |
] | |
} | |
], | |
"prompt_number": 46 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.arange(10) * np.arange(10, 20)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 47, | |
"text": [ | |
"array([ 0, 11, 24, 39, 56, 75, 96, 119, 144, 171])" | |
] | |
} | |
], | |
"prompt_number": 47 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"a = np.arange(8).reshape((4, 2))" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 107 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 108, | |
"text": [ | |
"array([[0, 1],", | |
" [2, 3],", | |
" [4, 5],", | |
" [6, 7]])" | |
] | |
} | |
], | |
"prompt_number": 108 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a * np.array([2, 3])" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 109, | |
"text": [ | |
"array([[ 0, 3],", | |
" [ 4, 9],", | |
" [ 8, 15],", | |
" [12, 21]])" | |
] | |
} | |
], | |
"prompt_number": 109 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a * np.array([[2], [3], [5], [6]])" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 52, | |
"text": [ | |
"array([[ 0, 2],", | |
" [ 6, 9],", | |
" [20, 25],", | |
" [36, 42]])" | |
] | |
} | |
], | |
"prompt_number": 52 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a * np.array([[1, 2], [3, 4]])" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"ename": "ValueError", | |
"evalue": "shape mismatch: objects cannot be broadcast to a single shape", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m/Users/mrdavis/projects/numpy_tutorial_2012-05-31/<ipython-input-53-b5bd2537fe31>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\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[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m: shape mismatch: objects cannot be broadcast to a single shape" | |
] | |
} | |
], | |
"prompt_number": 53 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 4. NumPy Functions ##", | |
"http://docs.scipy.org/doc/numpy/reference/ufuncs.html#available-ufuncs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 54, | |
"text": [ | |
"array([[0, 1],", | |
" [2, 3],", | |
" [4, 5],", | |
" [6, 7]])" | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.sin(a)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 56, | |
"text": [ | |
"array([[ 0. , 0.84147098],", | |
" [ 0.90929743, 0.14112001],", | |
" [-0.7568025 , -0.95892427],", | |
" [-0.2794155 , 0.6569866 ]])" | |
] | |
} | |
], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 5. Array Attributes & Methods ##", | |
"http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html#array-attributes", | |
"", | |
"http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html#array-methods" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 57, | |
"text": [ | |
"array([[0, 1],", | |
" [2, 3],", | |
" [4, 5],", | |
" [6, 7]])" | |
] | |
} | |
], | |
"prompt_number": 57 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.shape" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 58, | |
"text": [ | |
"(4, 2)" | |
] | |
} | |
], | |
"prompt_number": 58 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.size" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 59, | |
"text": [ | |
"8" | |
] | |
} | |
], | |
"prompt_number": 59 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.dtype" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 60, | |
"text": [ | |
"dtype('int64')" | |
] | |
} | |
], | |
"prompt_number": 60 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.nbytes" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 61, | |
"text": [ | |
"64" | |
] | |
} | |
], | |
"prompt_number": 61 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.reshape((2,4))" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 62, | |
"text": [ | |
"array([[0, 1, 2, 3],", | |
" [4, 5, 6, 7]])" | |
] | |
} | |
], | |
"prompt_number": 62 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.argsort()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 63, | |
"text": [ | |
"array([[0, 1],", | |
" [0, 1],", | |
" [0, 1],", | |
" [0, 1]])" | |
] | |
} | |
], | |
"prompt_number": 63 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.min()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 67, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 67 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.max()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 68, | |
"text": [ | |
"9" | |
] | |
} | |
], | |
"prompt_number": 68 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print a.mean(), a.sum(), a.std(), a.prod()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"4.5 45 2.87228132327 0" | |
] | |
} | |
], | |
"prompt_number": 69 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.mean([1, 2, 3, 4])" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 71, | |
"text": [ | |
"2.5" | |
] | |
} | |
], | |
"prompt_number": 71 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 6. numpy.random ##", | |
"http://docs.scipy.org/doc/numpy/reference/routines.random.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.random.permutation(np.arange(10))" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 110, | |
"text": [ | |
"array([2, 3, 5, 6, 8, 1, 0, 7, 9, 4])" | |
] | |
} | |
], | |
"prompt_number": 110 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.random.random((2, 2))" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 77, | |
"text": [ | |
"array([[ 0.50411342, 0.10708051],", | |
" [ 0.7061658 , 0.87775115]])" | |
] | |
} | |
], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.random.random_integers(5, 10, 5)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 79, | |
"text": [ | |
"array([7, 7, 5, 8, 9])" | |
] | |
} | |
], | |
"prompt_number": 79 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 7. Masked Arrays ##", | |
"http://docs.scipy.org/doc/numpy/reference/maskedarray.html", | |
"", | |
"http://docs.scipy.org/doc/numpy/reference/routines.ma.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"import numpy.ma as ma" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 80 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"a = ma.masked_greater(np.random.random(10), 0.5)" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 81 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 82, | |
"text": [ | |
"masked_array(data = [0.126459538933 -- -- 0.382754789138 0.0975191738884 0.182983512924", | |
" 0.367432667685 -- 0.0585601591978 --],", | |
" mask = [False True True False False False False True False True],", | |
" fill_value = 1e+20)" | |
] | |
} | |
], | |
"prompt_number": 82 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print a.mean(), a.max()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0.202618306961 0.382754789138" | |
] | |
} | |
], | |
"prompt_number": 84 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a[0] = ma.masked", | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[-- -- -- 0.382754789138 0.0975191738884 0.182983512924 0.367432667685 --", | |
" 0.0585601591978 --]" | |
] | |
} | |
], | |
"prompt_number": 85 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a[0] = 1.0", | |
"print a" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1.0 -- -- 0.382754789138 0.0975191738884 0.182983512924 0.367432667685 --", | |
" 0.0585601591978 --]" | |
] | |
} | |
], | |
"prompt_number": 87 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.filled()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 88, | |
"text": [ | |
"array([ 1.00000000e+00, 1.00000000e+20, 1.00000000e+20,", | |
" 3.82754789e-01, 9.75191739e-02, 1.82983513e-01,", | |
" 3.67432668e-01, 1.00000000e+20, 5.85601592e-02,", | |
" 1.00000000e+20])" | |
] | |
} | |
], | |
"prompt_number": 88 | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 8. Array Comparison ##", | |
"http://docs.scipy.org/doc/numpy/reference/routines.testing.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"a = np.arange(10)", | |
"b = np.arange(5, 25, 2)" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 89 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"b.shape" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 90, | |
"text": [ | |
"(10,)" | |
] | |
} | |
], | |
"prompt_number": 90 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print a", | |
"print b" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0 1 2 3 4 5 6 7 8 9]", | |
"[ 5 7 9 11 13 15 17 19 21 23]" | |
] | |
} | |
], | |
"prompt_number": 91 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a == b" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 93, | |
"text": [ | |
"array([False, False, False, False, False, False, False, False, False, False], dtype=bool)" | |
] | |
} | |
], | |
"prompt_number": 93 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.allclose(a, b)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 94, | |
"text": [ | |
"False" | |
] | |
} | |
], | |
"prompt_number": 94 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"a[1] = 7" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 95 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"(a == b).any()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 97, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 97 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"(a == b).all()" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 98, | |
"text": [ | |
"False" | |
] | |
} | |
], | |
"prompt_number": 98 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"np.testing.assert_allclose(a, b)" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"ename": "AssertionError", | |
"evalue": "\nNot equal to tolerance rtol=1e-07, atol=0\n\n(mismatch 100.0%)\n x: array([0, 7, 2, 3, 4, 5, 6, 7, 8, 9])\n y: array([ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m/Users/mrdavis/projects/numpy_tutorial_2012-05-31/<ipython-input-99-b55a17f72509>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtesting\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0massert_allclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m/usr/stsci/pyssgdev/2.7/numpy/testing/utils.pyc\u001b[0m in \u001b[0;36massert_allclose\u001b[0;34m(actual, desired, rtol, atol, err_msg, verbose)\u001b[0m\n\u001b[1;32m 1128\u001b[0m \u001b[0mheader\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Not equal to tolerance rtol=%g, atol=%g'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mrtol\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0matol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1129\u001b[0m assert_array_compare(compare, actual, desired, err_msg=str(err_msg),\n\u001b[0;32m-> 1130\u001b[0;31m verbose=verbose, header=header)\n\u001b[0m\u001b[1;32m 1131\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1132\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0massert_array_almost_equal_nulp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnulp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m/usr/stsci/pyssgdev/2.7/numpy/testing/utils.pyc\u001b[0m in \u001b[0;36massert_array_compare\u001b[0;34m(comparison, x, y, err_msg, verbose, header)\u001b[0m\n\u001b[1;32m 616\u001b[0m names=('x', 'y'))\n\u001b[1;32m 617\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcond\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 618\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAssertionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 619\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 620\u001b[0m msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,\n", | |
"\u001b[0;31mAssertionError\u001b[0m: \nNot equal to tolerance rtol=1e-07, atol=0\n\n(mismatch 100.0%)\n x: array([0, 7, 2, 3, 4, 5, 6, 7, 8, 9])\n y: array([ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])" | |
] | |
} | |
], | |
"prompt_number": 99 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [], | |
"language": "python", | |
"outputs": [] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment