{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"2020-10-20 - lesson #5 notebook.ipynb","provenance":[],"collapsed_sections":["8zA_bjo3ms-L","DvNPRiTdRlBW","nCN67JVO_cAt","udEkmS73_ggk","1AsOsSvFEB1m","-dHV11u5Ip4A","efrXHo-UONqE","7bNTnp6-Hiw7"],"authorship_tag":"ABX9TyNigPveCN38xHr8+AEqWKDa"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"AYMKepJSbk7_"},"source":["# Video lesson \\#5\n","NumPy: arrays and functions"]},{"cell_type":"markdown","metadata":{"id":"8zA_bjo3ms-L"},"source":["## Importing NumPy"]},{"cell_type":"code","metadata":{"id":"t0qhAmmpQAZg","executionInfo":{"status":"ok","timestamp":1602961264831,"user_tz":420,"elapsed":666,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"e5e34d3d-107c-4b09-dc77-7ef410702724","colab":{"base_uri":"https://localhost:8080/","height":55}},"source":["import numpy as np\n","\n","print(np.__version__)"],"execution_count":2,"outputs":[{"output_type":"stream","text":["1.18.5\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"DvNPRiTdRlBW"},"source":["## The NumPy array"]},{"cell_type":"code","metadata":{"id":"UDnlErdrQPUf","executionInfo":{"status":"ok","timestamp":1602961265137,"user_tz":420,"elapsed":963,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"154271c1-d11c-494b-ed4b-a7412aded50a","colab":{"base_uri":"https://localhost:8080/","height":196}},"source":["# Similarities between lists and NumPy 1-D arrays\n","\n","# Both are mutable, indexable, and sliceable\n","numbers = np.array([5,6,7,8])\n","numbers[1] = 13\n","print(numbers)\n","print(numbers[-3:])\n","\n","# Both are iterable\n","for num in numbers:\n"," print(num)\n","\n","# Find length using len()\n","print(len(numbers))\n","\n","# Check membership using in/not in\n","print(13 in numbers)\n","print(14 in numbers)"],"execution_count":3,"outputs":[{"output_type":"stream","text":["[ 5 13 7 8]\n","[13 7 8]\n","5\n","13\n","7\n","8\n","4\n","True\n","False\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"nCN67JVO_cAt"},"source":["## Differences between lists and NumPy 1-D arrays"]},{"cell_type":"code","metadata":{"id":"zv1jv8n_fZ41","executionInfo":{"status":"ok","timestamp":1602961265138,"user_tz":420,"elapsed":955,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"439977ef-429a-402b-880d-4e182547b32e","colab":{"base_uri":"https://localhost:8080/","height":178}},"source":["# Arrays can contain only a single object type\n","# Check using .dtype\n","numbers = np.array([5,6,7,8])\n","print(numbers.dtype)\n","print(numbers.astype(str))\n","\n","# Unlike lists, arrays preserve scientific notation\n","print([3.5e9,1.4e-3])\n","print(np.array([3.5e9,1.4e-3]))\n","\n","# NumPy has append() [note different syntax], insert(), delete(), flip() functions\n","# but no option to remove, pop (as with lists)\n","numbers = [5,6,7,8] # list version\n","numbers.append([9,10])\n","print(numbers)\n","\n","numbers = np.array([5,6,7,8]) # array version\n","numbers = np.append(numbers,[9,10])\n","print(numbers)\n","\n","# Convert between lists and arrays\n","my_list = [5,6,7,8]\n","my_array = np.array(my_list)\n","my_list1 = my_array.tolist()\n","my_list2 = list(my_array)\n","\n","# Adding lists concatenates them, while adding arrays actually adds them!\n","a = [1,2,3,4]\n","b = [5,6,7,8]\n","print(a + b)\n","\n","a = np.array([1,2,3,4])\n","b = np.array([5,6,7,8])\n","print(a + b)"],"execution_count":4,"outputs":[{"output_type":"stream","text":["int64\n","['5' '6' '7' '8']\n","[3500000000.0, 0.0014]\n","[3.5e+09 1.4e-03]\n","[5, 6, 7, 8, [9, 10]]\n","[ 5 6 7 8 9 10]\n","[1, 2, 3, 4, 5, 6, 7, 8]\n","[ 6 8 10 12]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"udEkmS73_ggk"},"source":["## Arithmetic operations with arrays"]},{"cell_type":"code","metadata":{"id":"WEU06MFkhth3","executionInfo":{"status":"ok","timestamp":1602961265138,"user_tz":420,"elapsed":947,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"f586fe6d-7c93-43e9-9083-a869024056f5","colab":{"base_uri":"https://localhost:8080/","height":90}},"source":["a = np.array([1,2,3,4])\n","b = np.array([5,6,7,8])\n","\n","print('a + b =',a + b)\n","print('a - b =',a - b)\n","print('a * b =',a * b)"],"execution_count":5,"outputs":[{"output_type":"stream","text":["a + b = [ 6 8 10 12]\n","a - b = [-4 -4 -4 -4]\n","a * b = [ 5 12 21 32]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"RzJnloscm0xz","executionInfo":{"status":"ok","timestamp":1602961265139,"user_tz":420,"elapsed":939,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"83672417-22a0-4437-aea3-d92396186603","colab":{"base_uri":"https://localhost:8080/","height":108}},"source":["print('a + 10 =',a + 10)\n","print('10 * a =',10 * a)\n","print('a / 10 =',a / 10)\n","print('a**2 =',a**2)"],"execution_count":6,"outputs":[{"output_type":"stream","text":["a + 10 = [11 12 13 14]\n","10 * a = [10 20 30 40]\n","a / 10 = [0.1 0.2 0.3 0.4]\n","a**2 = [ 1 4 9 16]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"1AsOsSvFEB1m"},"source":["## Element-wise operations require arrays of the same size"]},{"cell_type":"code","metadata":{"id":"MtYDq7Snq0zU","executionInfo":{"status":"ok","timestamp":1602961265139,"user_tz":420,"elapsed":938,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}}},"source":["x = np.array([1,2,3])\n","y = np.array([11,12,13,14,15])\n","\n","# print(x + y) # this will produce an error"],"execution_count":7,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"-dHV11u5Ip4A"},"source":["## Logical operations with arrays"]},{"cell_type":"code","metadata":{"id":"kCDzq69iEK1g","executionInfo":{"status":"ok","timestamp":1602961265140,"user_tz":420,"elapsed":930,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"d6b28bd9-f2b8-4b57-8df1-4bb6c6952db2","colab":{"base_uri":"https://localhost:8080/","height":108}},"source":["u = np.array([1,2,3,4])\n","v = np.array([0,2,4,6])\n","\n","print(u == v)\n","print(u < v)\n","print(v != 0)\n","print(v <= 4)"],"execution_count":8,"outputs":[{"output_type":"stream","text":["[False True False False]\n","[False False True True]\n","[False True True True]\n","[ True True True False]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"NxzqkWHfI8-3","executionInfo":{"status":"ok","timestamp":1602961265140,"user_tz":420,"elapsed":922,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"af48f2d1-2c91-4011-f192-1f8a0d48b899","colab":{"base_uri":"https://localhost:8080/","height":72}},"source":["bool1 = np.array([True,False,True])\n","bool2 = np.array([True,False,False])\n","\n","print(np.logical_and(bool1,bool2))\n","print(np.logical_or(bool1,bool2))"],"execution_count":9,"outputs":[{"output_type":"stream","text":["[ True False False]\n","[ True False True]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"efrXHo-UONqE"},"source":["## New indexing options with arrays"]},{"cell_type":"code","metadata":{"id":"3f0ZURQTKlP1","executionInfo":{"status":"ok","timestamp":1602963063592,"user_tz":420,"elapsed":587,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"1f1036a2-a4ab-4c60-e148-c2ccd80d18b4","colab":{"base_uri":"https://localhost:8080/","height":108}},"source":["v = np.array([10,11,12,13])\n","\n","print(v[3])\n","\n","print(v[[2,3]])\n","\n","print(v[v >= 12])\n","\n","print(v[[False,False,True,True]])"],"execution_count":16,"outputs":[{"output_type":"stream","text":["13\n","[12 13]\n","[12 13]\n","[12 13]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"JBFVTMiTMX1O","executionInfo":{"status":"ok","timestamp":1602963063852,"user_tz":420,"elapsed":378,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"e65f2aeb-096a-4292-d66c-12ef570fdbbe","colab":{"base_uri":"https://localhost:8080/","height":72}},"source":["print(np.where(v >= 12))\n","\n","print(np.where(v >= 12)[0])"],"execution_count":17,"outputs":[{"output_type":"stream","text":["(array([2, 3]),)\n","[2 3]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"7bNTnp6-Hiw7"},"source":["## NumPy functions and constants"]},{"cell_type":"code","metadata":{"id":"uz9OjKNlHluM","executionInfo":{"status":"ok","timestamp":1602969263354,"user_tz":420,"elapsed":1037,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"b652997a-0476-407a-dbbe-58ae5df1ebbb","colab":{"base_uri":"https://localhost:8080/","height":72}},"source":["x = np.array([10,11,12,13])\n","\n","# two ways of applying functions to arrays (or lists)\n","print(np.sum(x))\n","print(x.sum())"],"execution_count":68,"outputs":[{"output_type":"stream","text":["46\n","46\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"ePfhRUPTIl1R","executionInfo":{"status":"ok","timestamp":1602970238961,"user_tz":420,"elapsed":917,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"cf331b3f-84ef-4674-a0e4-1d30dbfc54af","colab":{"base_uri":"https://localhost:8080/","height":143}},"source":["# mathematical reductions\n","print(np.sum(x))\n","print(np.mean(x))\n","print(np.median(x))\n","print(np.max(x))\n","print(np.min(x))\n","print(np.std(x))"],"execution_count":74,"outputs":[{"output_type":"stream","text":["46\n","11.5\n","11.5\n","13\n","10\n","1.118033988749895\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"_nehgd3jafzM","executionInfo":{"status":"ok","timestamp":1602969277857,"user_tz":420,"elapsed":555,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"fcb50b10-014e-463d-88e5-14c390027434","colab":{"base_uri":"https://localhost:8080/","height":143}},"source":["# constants\n","print(np.pi)\n","print(np.e)\n","print(np.inf)\n","print(np.nan)\n","\n","print(5 * np.inf)\n","print(5 * np.nan)"],"execution_count":70,"outputs":[{"output_type":"stream","text":["3.141592653589793\n","2.718281828459045\n","inf\n","nan\n","inf\n","nan\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"ETLGD9j0ahM6","executionInfo":{"status":"ok","timestamp":1602969282964,"user_tz":420,"elapsed":576,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"4950ce35-1e7c-4f8f-9a61-64bb3e2d61bd","colab":{"base_uri":"https://localhost:8080/","height":143}},"source":["# mathematical element-wise functions\n","print(np.absolute([-2,-1]))\n","print(np.round([5.23,5.29],1))\n","print(np.sqrt([9,16])) # same as y**0.5\n","print(np.exp([0,1,2]))\n","print(np.sin([0,np.pi/2])) # argument: angles in radians\n","print(np.cos([np.pi,2*np.pi]))"],"execution_count":71,"outputs":[{"output_type":"stream","text":["[2 1]\n","[5.2 5.3]\n","[3. 4.]\n","[1. 2.71828183 7.3890561 ]\n","[0. 1.]\n","[-1. 1.]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"VD3yD93saicO","executionInfo":{"status":"ok","timestamp":1602969388214,"user_tz":420,"elapsed":1066,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"bd140f59-3444-4ab7-b61b-974664dcda60","colab":{"base_uri":"https://localhost:8080/","height":143}},"source":["# functions to create new arrays\n","print(np.zeros(4))\n","print(np.ones(4))\n","print(np.full(4,2))\n","print(np.arange(4))\n","print(np.arange(0,1,0.25))\n","print(np.linspace(0,1,5))"],"execution_count":73,"outputs":[{"output_type":"stream","text":["[0. 0. 0. 0.]\n","[1. 1. 1. 1.]\n","[2 2 2 2]\n","[0 1 2 3]\n","[0. 0.25 0.5 0.75]\n","[0. 0.25 0.5 0.75 1. ]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"wTSqAGE1a6vn"},"source":[""],"execution_count":null,"outputs":[]}]}