{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 9: Numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Version 1.0. Prepared by [Makzan](https://makzan.net). Updated at 2021 March." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy provides essential vector and matric computation. Numpy comes with its own array implementation similar to Python list. The difference is that numpy array has only one type for the entire array. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Numpy array creation](#Numpy-array-creation)\n", "- [Zeros, ones, full, random](b#Zeros,-ones,-full,-random)\n", "- [random seed](#random-seed)\n", "- [Creating array with linspace](#Creating-array-with-linspace)\n", "- [Reshaping array](#reshaping-array)\n", "- [Array operations](#Numpy-Operations)\n", "- [Array broadcast](#Array-broadcast)\n", "- [Querying Array](#Querying-the-array)\n", "- [Array slicing](#Array-Slicing)\n", "- [Reading CSV](#Reading-CSV)\n", "- [Dot Product](#Extra:-Dot-Product)\n", "- [Solving linear equations with numpy](#Extra:-Solving-linear-equations-with-Numpy)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numpy array creation" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5]\n" ] } ], "source": [ "arr1 = np.array([1,2,3,4,5])\n", "print(arr1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "array from range" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "arr2 = np.array(range(10))\n", "print(arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "array from range with `arange`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "arr2b = np.arange(10)\n", "print(arr2b)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10 11 12 13 14 15 16 17 18 19]\n" ] } ], "source": [ "arr2c = np.arange(10,20)\n", "print(arr2c)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 3 5 7 9 11 13 15 17 19]\n" ] } ], "source": [ "arr2d = np.arange(1,20,2)\n", "print(arr2d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can specify the data type by using `dtype`." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n" ] } ], "source": [ "arr3 = np.array(range(10), dtype='float')\n", "print(arr3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Please generate an array of [10,20,30,40,50,60,70,80,90,100], in int type." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Please generate an array of [10,20,30,40,50,60,70,80,90,100], in float type" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|array([ 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Zeros, ones, full, random" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" ] } ], "source": [ "arr6 = np.zeros(10)\n", "print(arr6)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 0 0 0 0 0 0 0 0]\n" ] } ], "source": [ "arr6b = np.zeros(10, dtype='int')\n", "print(arr6b)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n" ] } ], "source": [ "arr7 = np.ones(10, dtype='float')\n", "print(arr7)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3.14 3.14 3.14]\n" ] } ], "source": [ "arr8 = np.full(3, 3.14)\n", "print(arr8)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[3.14 3.14 3.14 3.14 3.14]\n", " [3.14 3.14 3.14 3.14 3.14]\n", " [3.14 3.14 3.14 3.14 3.14]]\n" ] } ], "source": [ "arr9 = np.full( (3,5), 3.14)\n", "print(arr9)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.79172504 0.52889492 0.56804456 0.92559664 0.07103606 0.0871293\n", " 0.0202184 0.83261985 0.77815675 0.87001215 0.97861834 0.79915856\n", " 0.46147936 0.78052918 0.11827443 0.63992102 0.14335329 0.94466892\n", " 0.52184832 0.41466194 0.26455561 0.77423369 0.45615033 0.56843395\n", " 0.0187898 0.6176355 0.61209572 0.616934 0.94374808 0.6818203\n", " 0.3595079 0.43703195 0.6976312 0.06022547 0.66676672 0.67063787\n", " 0.21038256 0.1289263 0.31542835 0.36371077 0.57019677 0.43860151\n", " 0.98837384 0.10204481 0.20887676 0.16130952 0.65310833 0.2532916\n", " 0.46631077 0.24442559 0.15896958 0.11037514 0.65632959 0.13818295\n", " 0.19658236 0.36872517 0.82099323 0.09710128 0.83794491 0.09609841\n", " 0.97645947 0.4686512 0.97676109 0.60484552 0.73926358 0.03918779\n", " 0.28280696 0.12019656 0.2961402 0.11872772 0.31798318 0.41426299\n", " 0.0641475 0.69247212 0.56660145 0.26538949 0.52324805 0.09394051\n", " 0.5759465 0.9292962 0.31856895 0.66741038 0.13179786 0.7163272\n", " 0.28940609 0.18319136 0.58651293 0.02010755 0.82894003 0.00469548\n", " 0.67781654 0.27000797 0.73519402 0.96218855 0.24875314 0.57615733\n", " 0.59204193 0.57225191 0.22308163 0.95274901]\n" ] } ], "source": [ "arr10 = np.random.rand(100)\n", "print(arr10)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.44712538 0.84640867 0.69947928]\n", " [0.29743695 0.81379782 0.39650574]\n", " [0.8811032 0.58127287 0.88173536]]\n" ] } ], "source": [ "arr10b = np.random.rand(3,3)\n", "print(arr10b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## random seed\n", "\n", "In programming language, random is not true random. We call it pseudorandom. Given the same seed, we can always generate the same sequence of numbers." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.5488135 ]\n", " [0.71518937]\n", " [0.60276338]\n", " [0.54488318]\n", " [0.4236548 ]\n", " [0.64589411]\n", " [0.43758721]\n", " [0.891773 ]\n", " [0.96366276]\n", " [0.38344152]]\n" ] } ], "source": [ "np.random.seed(0)\n", "arr11 = np.random.rand(10,1)\n", "print(arr11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we try to keep executing the following random function, we will keep getting new random numbers. But indeed, they are following the same sequence given the same seed.\n", "\n", "Try re-running the previous seed and we will get the same sequence again." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[0.79172504],\n", " [0.52889492],\n", " [0.56804456],\n", " [0.92559664],\n", " [0.07103606],\n", " [0.0871293 ],\n", " [0.0202184 ],\n", " [0.83261985],\n", " [0.77815675],\n", " [0.87001215]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(10,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**: \n", "Pleaes try using the seed 540, and see if you can generate the following expected result." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.5488135 ]\n", " [0.71518937]\n", " [0.60276338]\n", " [0.54488318]\n", " [0.4236548 ]\n", " [0.64589411]\n", " [0.43758721]\n", " [0.891773 ]\n", " [0.96366276]\n", " [0.38344152]]\n" ] } ], "source": [ "np.random.seed(0)\n", "arr = np.random.rand(10,1)\n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| Expected Result for seed(540) |\n", "| --- |\n", "| [[0.71688165]\n", " [0.50553693]\n", " [0.18142109]\n", " [0.70069925]\n", " [0.81784415]\n", " [0.28708016]\n", " [0.97490719]\n", " [0.09495503]\n", " [0.84069722]\n", " [0.06900928]]|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating array with linspace" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 5. 10.]\n" ] } ], "source": [ "arr4 = np.linspace(0,10,3)\n", "print(arr4)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 25. 50. 75. 100.]\n" ] } ], "source": [ "arr4b = np.linspace(0,100,5)\n", "print(arr4b)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.33333333 0.66666667 1. ]\n" ] } ], "source": [ "arr4c = np.linspace(0,1,4)\n", "print(arr4c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## reshaping array" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n" ] } ], "source": [ "arr5 = np.arange(1,13).reshape([3,4])\n", "print(arr5)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 4)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr5.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Try to create a random array with shape (5,4)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "scrolled": true }, "outputs": [], "source": [ "np.random.seed(0) # Reset the seed, in order to re-create the same result.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|array([[0.5488135 , 0.71518937, 0.60276338, 0.54488318],\n", " [0.4236548 , 0.64589411, 0.43758721, 0.891773 ],\n", " [0.96366276, 0.38344152, 0.79172504, 0.52889492],\n", " [0.56804456, 0.92559664, 0.07103606, 0.0871293 ],\n", " [0.0202184 , 0.83261985, 0.77815675, 0.87001215]])|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Try to create a one's array with shape (5,4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Operations" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "grid = np.arange(1,10).reshape([3,3])\n", "print(grid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tile" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n" ] } ], "source": [ "grid2 = np.arange(1,4)\n", "print(grid2)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [1 2 3]\n", " [1 2 3]]\n" ] } ], "source": [ "grid2 = np.tile(grid2, (3,1))\n", "print(grid2)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2 4 6]\n", " [ 5 7 9]\n", " [ 8 10 12]]\n" ] } ], "source": [ "print(grid+grid2)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 0 0]\n", " [3 3 3]\n", " [6 6 6]]\n" ] } ], "source": [ "print(grid-grid2)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 4 9]\n", " [ 4 10 18]\n", " [ 7 16 27]]\n" ] } ], "source": [ "print(grid*grid2)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1. ]\n", " [4. 2.5 2. ]\n", " [7. 4. 3. ]]\n" ] } ], "source": [ "print(grid/grid2)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 1 1]\n", " [4 2 2]\n", " [7 4 3]]\n" ] } ], "source": [ "print(grid//grid2)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 4 27]\n", " [ 4 25 216]\n", " [ 7 64 729]]\n" ] } ], "source": [ "print(grid ** grid2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array broadcast" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "grid = np.arange(1,10).reshape([3,3])\n", "print(grid)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid.shape" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 4 5 6]\n", " [ 7 8 9]\n", " [10 11 12]]\n" ] } ], "source": [ "print(grid + 3)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 6 9]\n", " [12 15 18]\n", " [21 24 27]]\n" ] } ], "source": [ "print(grid*3)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.1 0.2 0.3]\n", " [0.4 0.5 0.6]\n", " [0.7 0.8 0.9]]\n" ] } ], "source": [ "print(grid/10)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.33333333 0.66666667 1. ]\n", " [1.33333333 1.66666667 2. ]\n", " [2.33333333 2.66666667 3. ]]\n" ] } ], "source": [ "print(grid/3)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 0 1]\n", " [1 1 2]\n", " [2 2 3]]\n" ] } ], "source": [ "print(grid//3)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2 3 4]\n", " [ 5 6 7]\n", " [ 8 9 10]]\n" ] } ], "source": [ "print(grid+1)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n" ] } ], "source": [ "grid2 = np.arange(1,4)\n", "print(grid2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid2.shape" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "grid = np.arange(1,10).reshape([3,3])\n", "print(grid)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2 4 6]\n", " [ 5 7 9]\n", " [ 8 10 12]]\n" ] } ], "source": [ "print(grid+grid2)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 4 9]\n", " [16 25 36]\n", " [49 64 81]]\n" ] } ], "source": [ "print(grid ** 2)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 0 1]\n", " [2 3 4]]\n" ] } ], "source": [ "print(grid % 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Querying the array" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.5488135 0.71518937 0.60276338 ... 0.75842952 0.02378743 0.81357508]\n" ] } ], "source": [ "arr = np.random.random(10000)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4964.588916200894\n" ] } ], "source": [ "print(np.sum(arr))" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9999779517807228\n" ] } ], "source": [ "print(np.max(arr))" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.2449638492178e-05\n" ] } ], "source": [ "print(np.min(arr))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.49645889162008944\n" ] } ], "source": [ "print(np.mean(arr))" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.49350103035904186\n" ] } ], "source": [ "print(np.median(arr))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2060\n" ] } ], "source": [ "print(len(arr[arr<0.2]))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "995\n" ] } ], "source": [ "print(len(arr[(arr>0.2) & (arr<0.3)]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**: Given the following numpy array, please find all the records with negative value." ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "arr = np.array((-3, 10, 20, -5, -2, 50, 34, -12, 10))" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ -3, 10, 20, -5, -2, 50, 34, -12, 10])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected Result|\n", "|---|\n", "|array([ -3, -5, -2, -12])|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Slicing\n", "\n", "**Slicing in NumPy array is NOT COPY.**" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "# [i, j]\n", "# [i, :]\n", "# [:, j]\n", "# [i_start:i_end, j_start:j_end]" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n" ] } ], "source": [ "grid = np.arange(1,13).reshape([3,4])\n", "print(grid)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n" ] } ], "source": [ "print(grid[0,:])" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 5 9]\n" ] } ], "source": [ "print(grid[:,0])" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2 3]\n", " [ 6 7]\n", " [10 11]]\n" ] } ], "source": [ "print(grid[:,1:3])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "grid2 = grid[:,:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s see if the slicing is a copy or not:" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[100 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n", "[[100 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]]\n" ] } ], "source": [ "grid[0,0] = 100\n", "\n", "print(grid)\n", "\n", "print(grid2)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[100 99 99 4]\n", " [ 5 99 99 8]\n", " [ 9 99 99 12]]\n", "[[100 99 99 4]\n", " [ 5 99 99 8]\n", " [ 9 99 99 12]]\n" ] } ], "source": [ "grid[:,1:3] = 99\n", "\n", "print(grid)\n", "\n", "print(grid2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading CSV" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('2018-12-18', 22) ('2018-12-17', 0) ('2018-12-16', 4)\n", " ('2018-12-15', 218) ('2018-12-14', 11) ('2018-12-13', 11)\n", " ('2018-12-12', 14) ('2018-12-11', 4) ('2018-12-10', 5)\n", " ('2018-12-09', 15) ('2018-12-08', 104) ('2018-12-07', 19)\n", " ('2018-12-06', 8) ('2018-12-05', 3) ('2018-12-04', 24)\n", " ('2018-12-03', 66) ('2018-12-02', 40) ('2018-12-01', 69)\n", " ('2018-11-30', 8) ('2018-11-29', 13) ('2018-11-28', 10)\n", " ('2018-11-27', 18) ('2018-11-26', 72) ('2018-11-25', 31)\n", " ('2018-11-24', 146) ('2018-11-23', 42) ('2018-11-22', 56)\n", " ('2018-11-21', 19) ('2018-11-20', 76) ('2018-11-19', 11)\n", " ('2018-11-18', 0) ('2018-11-17', 0) ('2018-11-16', 6)\n", " ('2018-11-15', 7) ('2018-11-14', 32) ('2018-11-13', 102)\n", " ('2018-11-12', 198) ('2018-11-11', 22) ('2018-11-10', 82)\n", " ('2018-11-09', 213) ('2018-11-08', 52) ('2018-11-07', 13)\n", " ('2018-11-06', 0) ('2018-11-05', 6) ('2018-11-04', 0)\n", " ('2018-11-03', 7) ('2018-11-02', 25) ('2018-11-01', 29)\n", " ('2018-10-31', 9) ('2018-10-30', 14) ('2018-10-29', 4)\n", " ('2018-10-28', 4)]\n" ] } ], "source": [ "data = np.genfromtxt('visitors.csv',delimiter=',', dtype='datetime64[D],uint8', skip_header=0, names=('date','visitors'))\n", "print(data)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['2018-12-18', '2018-12-17', '2018-12-16', '2018-12-15',\n", " '2018-12-14', '2018-12-13', '2018-12-12', '2018-12-11',\n", " '2018-12-10', '2018-12-09', '2018-12-08', '2018-12-07',\n", " '2018-12-06', '2018-12-05', '2018-12-04', '2018-12-03',\n", " '2018-12-02', '2018-12-01', '2018-11-30', '2018-11-29',\n", " '2018-11-28', '2018-11-27', '2018-11-26', '2018-11-25',\n", " '2018-11-24', '2018-11-23', '2018-11-22', '2018-11-21',\n", " '2018-11-20', '2018-11-19', '2018-11-18', '2018-11-17',\n", " '2018-11-16', '2018-11-15', '2018-11-14', '2018-11-13',\n", " '2018-11-12', '2018-11-11', '2018-11-10', '2018-11-09',\n", " '2018-11-08', '2018-11-07', '2018-11-06', '2018-11-05',\n", " '2018-11-04', '2018-11-03', '2018-11-02', '2018-11-01',\n", " '2018-10-31', '2018-10-30', '2018-10-29', '2018-10-28'],\n", " dtype='datetime64[D]')" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['date']" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 22, 0, 4, 218, 11, 11, 14, 4, 5, 15, 104, 19, 8,\n", " 3, 24, 66, 40, 69, 8, 13, 10, 18, 72, 31, 146, 42,\n", " 56, 19, 76, 11, 0, 0, 6, 7, 32, 102, 198, 22, 82,\n", " 213, 52, 13, 0, 6, 0, 7, 25, 29, 9, 14, 4, 4],\n", " dtype=uint8)" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['visitors']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "What is the shape of the loaded CSV `data`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|(52,)|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the last 3 records in the data?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|array([('2018-10-30', 14), ('2018-10-29', 4), ('2018-10-28', 4)],\n", " dtype=[('date', '