{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(5)\n", "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 2 4 6 8]\n", "[3 4 5 6 7]\n", "[-2 -1 0 1 2]\n" ] } ], "source": [ "print(a*2)\n", "print(a+3)\n", "print(a-2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 1., 1., 1., 1.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.ones(5)\n", "b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-2. -1. 0. 1. 2.]\n", "[ 1. 2. 3. 4. 5.]\n" ] } ], "source": [ "print(a-b*2)\n", "print(a+b)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 6.27 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "100000 loops, best of 3: 11.4 µs per loop\n" ] } ], "source": [ "# these operations are faster in numpy than pure python\n", "\n", "a = np.arange(10000)\n", "%timeit a+2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000 loops, best of 3: 1.03 ms per loop\n" ] } ], "source": [ "# in native python\n", "a = range(10000)\n", "%timeit [i+2 for i in a]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Multiplication" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1-D array" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 8, 3, 4, 2])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.random.randint(1, 10, 5)\n", "a" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([6, 3, 9, 3, 4])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b =np.random.randint(1, 10, 5)\n", "b " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([12, 24, 27, 12, 8])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * b # elementwise operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2-D array" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[9, 5, 5],\n", " [7, 2, 4]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.random.randint(1, 10, (2,3))\n", "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[2, 1, 5],\n", " [5, 2, 7]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.random.randint(1, 10, (2,3))\n", "b" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[18, 5, 25],\n", " [35, 4, 28]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * b # element wise multiplication" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# a.dot(b) this will throw an error as Matix multiplication is not possible here" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[48, 90],\n", " [36, 67]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dot(b.T) # b.T will transpose the 2,3 matrix to 3,2 matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Comparison" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 3 7 6 5] [1 2 7 8 5]\n" ] } ], "source": [ "a = np.array([1, 3, 7, 6, 5])\n", "b = np.array([1, 2, 7, 8, 5])\n", "\n", "print(a,b)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ True, False, True, False, True], dtype=bool)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Comparison\n", "a == b" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([False, True, False, False, False], dtype=bool)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a > b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Arraywise Comparison" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = np.array([1,2,3,4])\n", "b = np.array([3,4, 9, 8])\n", "c = np.array([1,2,3,4])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array_equal(a, b)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array_equal(a, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Logical Operations" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ True True False False] [ True False True False]\n" ] } ], "source": [ "a = np.array([1,1,0,0], dtype='bool')\n", "b = np.array([1,0,1,0], dtype='bool')\n", "\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True, False], dtype=bool)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.logical_or(a,b)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ True, False, False, False], dtype=bool)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.logical_and(a,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Adv mathematics func" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 0.5, 0. , 3. ])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([1, 0.5, 0, 3])\n", "a" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.84147098, 0.47942554, 0. , 0.14112001])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(a)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\tools\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:1: RuntimeWarning: divide by zero encountered in log\n", " if __name__ == '__main__':\n" ] }, { "data": { "text/plain": [ "array([ 0. , -0.69314718, -inf, 1.09861229])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log(a)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 2.71828183, 1.64872127, 1. , 20.08553692])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(a)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1., 1.],\n", " [ 0., 0., 1.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.triu(np.ones((3, 3)), 1)\n", "a" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 1., 0., 0.],\n", " [ 1., 1., 0.]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.T # transposition of array is just a view and stored in memory" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Counting Sums" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([1,2,3,4])\n", "x.sum()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [3, 4, 5]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([[1,2,3],[3,4,5]])\n", "x" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([4, 6, 8])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum(axis=0) # column sum" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 6, 12])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum(axis=1) # row sum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Other functions" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.min()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 3])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.min(axis=1)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.max(axis=0)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.argmin() # index of min element" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.argmax() # index of max element" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Logical Operations" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all([True, False, True])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.any([True, False, True])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.zeros((10,10))\n", "np.any(a)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all(a)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.all(a==0)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.any(a!=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Stats" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 1] \n", "\n", " [[1 2 3]\n", " [5 6 1]]\n" ] } ], "source": [ "x = np.array([1, 2, 3, 1])\n", "y = np.array([[1, 2, 3], [5, 6, 1]])\n", "\n", "print(x, \"\\n\\n\",y)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.75" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.mean()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.median(x)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 2., 5.])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.median(y, axis=-1) # last axis" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.82915619758884995" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### broadcasing" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1., 1.],\n", " [ 1., 1., 1., 1.],\n", " [ 1., 1., 1., 1.]])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.ones((3,4))\n", "a" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1., 1.],\n", " [ 1., 1., 1., 1.],\n", " [ 1., 1., 0., 0.]])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:,2:]=0\n", "a" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(4,)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(0, 40, 10)\n", "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### newaxis" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0],\n", " [10],\n", " [20],\n", " [30]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = a[:, np.newaxis] # newaxis is a nice feature supported by numpy\n", "a" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(4, 1)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mileposts = np.array([0, 198, 303, 736, 871, 1175, 1475, 1544,1913, 2448])" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 198, 303, 736, 871, 1175, 1475, 1544, 1913, 2448],\n", " [ 198, 0, 105, 538, 673, 977, 1277, 1346, 1715, 2250],\n", " [ 303, 105, 0, 433, 568, 872, 1172, 1241, 1610, 2145],\n", " [ 736, 538, 433, 0, 135, 439, 739, 808, 1177, 1712],\n", " [ 871, 673, 568, 135, 0, 304, 604, 673, 1042, 1577],\n", " [1175, 977, 872, 439, 304, 0, 300, 369, 738, 1273],\n", " [1475, 1277, 1172, 739, 604, 300, 0, 69, 438, 973],\n", " [1544, 1346, 1241, 808, 673, 369, 69, 0, 369, 904],\n", " [1913, 1715, 1610, 1177, 1042, 738, 438, 369, 0, 535],\n", " [2448, 2250, 2145, 1712, 1577, 1273, 973, 904, 535, 0]])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distance = np.abs(mileposts - mileposts[:, np.newaxis])\n", "distance" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4] \n", "\n", " [[0]\n", " [1]\n", " [2]\n", " [3]\n", " [4]]\n" ] } ], "source": [ "x, y = np.arange(5), np.arange(5)[:, np.newaxis]\n", "\n", "print(x, \"\\n\\n\", y)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0. , 1. , 2. , 3. , 4. ],\n", " [ 1. , 1.41421356, 2.23606798, 3.16227766, 4.12310563],\n", " [ 2. , 2.23606798, 2.82842712, 3.60555128, 4.47213595],\n", " [ 3. , 3.16227766, 3.60555128, 4.24264069, 5. ],\n", " [ 4. , 4.12310563, 4.47213595, 5. , 5.65685425]])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distance = np.sqrt(x ** 2 + y ** 2)\n", "distance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "the numpy.ogrid function allows to directly create vectors x and y of the previous example, with two\n", "\"significant dimensions\":" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0]\n", " [1]\n", " [2]\n", " [3]\n", " [4]] \n", "\n", " [[0 1 2 3 4]]\n" ] } ], "source": [ "x, y = np.ogrid[0:5, 0:5]\n", "\n", "print(x, \"\\n\\n\", y)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(5, 1) (1, 5)\n" ] } ], "source": [ "print(x.shape, y.shape)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0. , 1. , 2. , 3. , 4. ],\n", " [ 1. , 1.41421356, 2.23606798, 3.16227766, 4.12310563],\n", " [ 2. , 2.23606798, 2.82842712, 3.60555128, 4.47213595],\n", " [ 3. , 3.16227766, 3.60555128, 4.24264069, 5. ],\n", " [ 4. , 4.12310563, 4.47213595, 5. , 5.65685425]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distance = np.sqrt(x ** 2 + y ** 2)\n", "distance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "np.mgrid\n", "directly providesmatrices full of indices for cases where we can’t (or don’t want to) benefit frombroadcasting" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 0 0 0]\n", " [1 1 1 1]\n", " [2 2 2 2]\n", " [3 3 3 3]] \n", "\n", " [[0 1 2 3]\n", " [0 1 2 3]\n", " [0 1 2 3]\n", " [0 1 2 3]]\n" ] } ], "source": [ "x, y = np.mgrid[0:4, 0:4]\n", "print(x, \"\\n\\n\", y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Flattening the array\n", "Higher dimensions: last dimensions ravel out “first”." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]])\n", "a" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel()" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 4, 2, 5, 3, 6])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.T.ravel()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reshaping\n", "reshaping may return view or copy, so use caustiously" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel()" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel().reshape(3,2)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.reshape((2, -1)) # # unspecified (-1) value is inferred" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.reshape((3, -1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Adding a dimention" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(4)\n", "a" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0],\n", " [1],\n", " [2],\n", " [3]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:, np.newaxis]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[np.newaxis,:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dimension shuffling" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1],\n", " [ 2, 3],\n", " [ 4, 5]],\n", "\n", " [[ 6, 7],\n", " [ 8, 9],\n", " [10, 11]],\n", "\n", " [[12, 13],\n", " [14, 15],\n", " [16, 17]],\n", "\n", " [[18, 19],\n", " [20, 21],\n", " [22, 23]]])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(4*3*2).reshape(4, 3, 2)\n", "a" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(4, 3, 2)" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0,1,0]" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]],\n", "\n", " [[12, 13, 14, 15],\n", " [16, 17, 18, 19],\n", " [20, 21, 22, 23]]])" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel().reshape(2,3,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Resizing" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 0, 0, 0, 0])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "o = np.arange(4)\n", "o.resize((8,))\n", "o\n", "\n", "# It will throw an error \n", "# ValueError: cannot resize an array that references or is referenced\n", "# by another array in this way. Use the resize function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Sorting data" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[4, 3, 5],\n", " [1, 2, 1]])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[4, 3, 5], [1, 2, 1]])\n", "a" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[3, 4, 5],\n", " [1, 1, 2]])" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.sort(a, axis=1) # Sorts each row separately!\n", "b" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[3, 4, 5],\n", " [1, 1, 2]])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sort(axis=1) # inplace sort\n", "a" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 1, 0], dtype=int64)" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sorting with fancy indexing:\n", "a = np.array([4, 3, 1, 2])\n", "j = np.argsort(a)\n", "j" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[j]" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 2\n", "4 1\n" ] } ], "source": [ "# Finding minima and maxima\n", "j_max = np.argmax(a)\n", "j_min = np.argmin(a)\n", "\n", "print(j_max, j_min) # indexes\n", "print(a[j_max], a[j_min])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }