{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## an array looks like a Python list that has different shapes\n", "- That is why you see the square brackets\n", "- Array is just a group of items of the same type" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## [Importing into array /exporting array](#Importing-into-array/-exporting-array)\n", "\n", "- np.loadtxt('./data/data1.txt')\n", "- np.savetxt('./data/ar1.txt',ar1,delimiter=' ')\n", "***\n", "\n", "\n", "## [Convert from pandas](#convert-from-pandas)\n", "- df.values\n", "\n", "
\n", "Tip: to python list\n", "ar.to_list()\n", "
\n", "\n", "***\n", "\n", "## [Creating arrays from scratch](#from-scratch)\n", "\n", "> ### 1D\n", "- np.array([1,2,3, 10])\n", "- np.zeros(how_many)\n", "- np.ones(how_many)\n", "- np.full((what), how_many)\n", "\n", "> ### 2+D\n", "### np.array([(), ()]), or np.array([[ , , ]])\n", "\n", "> ### [np.linspace](#np-linspace)\n", "> arithmetics interval = (end - start)/(n -1). \n", "> For example, (10-1)/(4-1) = 3\n", "\n", "> ### [np.arange](#np-arange)\n", "> ### any dimension\n", "np.arange(some_size).reshape(some_shape)\n", "\n", "\n", "
\n", "Tip: np.arange is similar to np.linspace with following 3 differences:\n", "1. end point is not included; 2. you specify the interval; 3.np.linspace returns array of floats whereas np,.arange returns integers\n", "
\n", "\n", "> ### [np.random](#np-random)\n", "- np.random.rand(n,m) retarts array with floats between 0-1\n", "- np.random.rand(n,m)*k retarts array with floats between 0-k\n", "- np.random.randint(start, end, size=(n,m)) If we do not specify size =, it will return 1 random integer between start and end; np.random.randint(0,10,size=(2,3))\n", "- np.random.randn(n,m) returns array with floats from standard normal distribution with mean 0 and standard deviation of 1\n", "- np.random.randn(n,m) * sigma + mu, returns array with floats shifted and scaled from the standard normal distribution\n", "\n", "***\n", "\n", "> ### [Inspecting array](#inspect)\n", "- ar.size | number of elements in ar\n", "- ar.shape | dimensions of ar (rows,columns)\n", "- ar.ndim | number of dimensions\n", "- ar.dtype | type of elements in ar\n", "- ar.astype(dtype) | Convert arr elements to type dtype\n", "***\n", "\n", "\n", "## [Copying/sorting/reshaping](#copy)\n", "- np.copy(ar) \t| copies to *new* memory, i.e. deep copy. \n", " In comparison,ar2 = ar1 is a shallow copy because changing ar1 will change ar2, and vice versa! Because shallow copy is just giving the original array another name, nothing else. \n", "- ar.sort() \t| sort\n", "\n", "- ar.sort(axis=0) \n", "\n", "\n", "- two_d_ar.flatten() \t| Flattens 2D aray to 1D\n", "\n", "\n", "- ar.T \t| Transposes ar (rows become columns and vice versa)\n", "\n", "\n", "- ar.reshape(3,4) \t| Reshapes ar to 3 rows, 4 columns without changing data\n", "\n", "\n", "- ar.resize((5,6)) \t| Changes ar shape to 5x6 and fills new values with 0\n", "\n", "- ar.view(dtype) \t| Creates view of ar elements with type dtype\n", "
\n", "Tip: b = array.copy() is a deep copy (i.e. new memory)\n", "This is very different from python list. Copied python list is not a deep copy unless you make it a deep copy using import copy.\n", "
\n", "\n", "***\n", "\n", "> ## [Combining/splitting](#Combining-splitting)\n", "- np.concatenate((ar1,ar2),axis=0) | Adds ar2 as rows to the end of ar1\n", "- np.concatenate((ar1,ar2),axis=1) | Adds ar2 as columns to end of ar1\n", "- np.split(ar,3) | Splits ar into 3 sub-arays\n", "- np.hsplit(ar,5) | Splits ar horizontally on the 5th index\n", "\n", "***\n", "\n", "> ## [Adding/removing Elements](#add-remove)\n", "- np.append(arr,values) | Appends values to end of arr\n", "- np.insert(arr,2,values) | Inserts values into arr before index 2\n", "- np.delete(arr,3,axis=0) | Deletes row on index 3 of arr\n", "- np.delete(arr,4,axis=1) | Deletes column on index 4 of arr\n", "\n", "***\n", "\n", "> ## [Indexing/slicing/subsetting](#indexing-slicing-subsetting)\n", "- ar[5] | the element at index 5\n", "- ar[2,5] | element on index [2][5]\n", "- ar[1]=4 | Assigns aray element on index 1 the value 4\n", "- ar[0:3] | Returns the elements at indices 0,1,2 (On a 2D aray: returns rows 0,1,2)\n", "- ar[0:3,4] | Returns the elements on rows 0,1,2 at column 4\n", "- ar[:2] | Returns the elements at indices 0,1 (On a 2D aray: returns rows 0,1)\n", "- ar[:,1] | Returns the elements at index 1 on all rows\n", "- ar\\\\<5 | Returns an aray with boolean values\n", "- (ar1\\\\<3) & (ar2\\\\>5) | Returns an aray with boolean values\n", "- ~ar | Inverts a boolean aray\n", "\n", "- ar[ar<5] | boolean slicing\n", "\n", "\n", "## [More examples](#more-exaples)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "\n", "## Importing into array /exporting array\n", "\n", "#### txt file (data separated with space)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 2., 3.],\n", " [4., 5., 6.]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#data1.txt just have 6 numbers, separated with spaces, and a line breaker\n", "ar1 = np.loadtxt('./data/data1.txt')\n", "ar1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#save txt file\n", "np.savetxt('./data/ar1.txt',ar1,delimiter=' ')" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 2., 3.],\n", " [4., 5., 6.]])" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#re-import it to verfiy it is working\n", "ar1 = np.loadtxt('./data/ar1.txt')\n", "ar1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Convert from pandas\n", "- df.values\n", "\n", "## Convert to list\n", "- arr.tolist() | Convert arr to a Python list" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0123
06231
14543
\n", "
" ], "text/plain": [ " 0 1 2 3\n", "0 6 2 3 1\n", "1 4 5 4 3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('./data/data2.csv', header=None)\n", "df" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6, 2, 3, 1],\n", " [4, 5, 4, 3]], dtype=int64)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numpy_matrix = df.values\n", "numpy_matrix" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AB
013
124
\n", "
" ], "text/plain": [ " A B\n", "0 1 3\n", "1 2 4" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({\"A\": [1, 2], \"B\": [3, 4]})\n", "df" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 3],\n", " [2, 4]], dtype=int64)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar = df.values\n", "ar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert to python list\n", "- we already know numpy.array converts python list to numpy array\n", "- arr.tolist() | Convert arr to a Python list" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 100],\n", " [ 2, 1]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar = np.array([[1,100],[2,1]])\n", "ar" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 100], [2, 1]]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ar.tolist()\n", "l" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## From Scrach\n", "### 1D\n", "- np.array([1,2,3, 10])\n", "- np.zeros(how_many)\n", "- np.ones(how_many)\n", "- np.full((what), how_many)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 7., 90., 1000.])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,7,90,1000.])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(100)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones(10)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.full((1),1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2D\n", "### np.array([(), ()]), or np.array([[ , , ]])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((10,10))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((10,10))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1000.1, 1000.1, 1000.1],\n", " [1000.1, 1000.1, 1000.1]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.full((2,3),1000.1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([(1,2),(3,4)])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([(1,7),(6,9)]).shape" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 0]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([(1,2),(3,0)])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2,)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1,2,3,4],[1,4,3]]).shape" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 4)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1,2,3,4],[5,6,7,8]]).shape\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 4]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1,2,3,4]])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([(1,2),(3,4)])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [3, 4, 5],\n", " [1, 2, 3]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# nested lists result in multi-dimensional arrays\n", "np.array([range(i, i + 3) for i in [1, 3, 1]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## np.linspace " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1. , 1.33333333, 1.66666667, 2. ])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#np.linspace arithmetics interval = (end - start)/(n -1). \n", "# For example, (10-1)/(4-1) = 3\n", "np.linspace(1,2,4)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 3., 5., 7., 9., 11.])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#interval = (11-1)/(6-1)= 2\n", "np.linspace(1,11,6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.arange " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 3, 6, 9])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# np.arange is similar to np.linspace with following 3 differences:\n", "# 1. end point is not included; 2. you specify the interval; 3.np.linspace returns array of floats whereas np,.arange returns integers\n", "# in general, I prefer np.arange instead of np.linspace\n", "np.arange(0,10,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.random \n", "- np.random.rand(n,m) retarts array with floats between 0-1\n", "- np.random.rand(n,m)*k retarts array with floats between 0-k\n", "- np.random.randint(start, end, size=(n,m)) If we do not specify size =, it will return 1 random integer between start and end; np.random.randint(0,10,size=(2,3))\n", "- np.random.randn(n,m) returns array with floats from standard normal distribution with mean 0 and standard deviation of 1\n", "- np.random.randn(n,m) * sigma + mu, returns array with floats shifted and scaled from the standard normal distribution" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.57256735, 0.50921722, 0.11257014],\n", " [0.17108578, 0.62249645, 0.52027419]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(2,3) # 2X3 array of random floats between 0–1" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[80.97626817, 84.65068553, 28.65173649],\n", " [62.53292734, 71.53976566, 47.46193707]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(2,3)*100 # 2X3 array of random floats between 0–100" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(2,100)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([0., 0., 0., 0., 0., 0., 0., 4., 2., 2., 0., 1., 0., 1., 0., 0., 0.,\n", " 0., 0., 0.]),\n", " array([-30, -27, -24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6,\n", " 9, 12, 15, 18, 21, 24, 27, 30]),\n", " )" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9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xqxS8DGjeWrs+7gSMMxgOrcCx9xjHqbSPWrfj3ZIy1WHOzkjwd+DTwjgW/ta8pVfWLqjqdwW/nZzE4lfmkbtM+bq/r3I+nqnplz67/AtwIvId+j0g47saNJcnFwKuAV1R38o1VOJYJvifDVt04eliLNY/zgyQbq+pgd6rywZUuqI8kGxgE+7VV9Zlu9ZocyxOq6pEktzI4535ikvXd7H1Zfs5W3cz9WJKcOrR4IXBv194FvL67auZs4NEnfn1brZKcC/w1cGFV/WRo0y5gWwYfgHIKcCpw+0rUuERrcRx9HrWx1gw/GuRi4N9XsJZekoTBXe/7qur9Q5vW4lhmnrgSLsnTgFcy+BvCVxg8qgWWaywr/dfkCf/y/GngbmAv8Dlg09BfpD/M4FzWtxi6amO1vhj8gfEBYE/3umpo27u7sdwHnLfStY4Zx58xmPE+DvwA+MJaHMdQzeczuDrju8C7V7qeCWv/JHAQ+J/ue/ImBo/e/g/gO93XZ610nT3G8QcMTlPsHfr3cf4aHcuLgG92Y7kbuLxb/zwGk539wL8CJ0z72N6hKkkNWlOnZSRJ/RjuktQgw12SGmS4S1KDDHdJapDhLkkNMtwlqUGGuyQ16H8BqPckA8nuLasAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "ar = np.random.randn(10)*10+1\n", "plt.hist(ar, bins = np.arange(-30,31,3))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.758782594543135" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.std() # Returns the standard deviation of the array elements along given axis." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "ar = np.random.randn(1000)*10+1" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([ 1., 5., 8., 20., 30., 33., 72., 82., 93., 118., 120.,\n", " 106., 117., 81., 52., 28., 13., 15., 5., 1.]),\n", " array([-30, -27, -24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6,\n", " 9, 12, 15, 18, 21, 24, 27, 30]),\n", " )" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAD65JREFUeJzt3X+sX3V9x/Hnay0wf8wU5EJqC2tNGiczbpIbwuZijJjJD0NZIkmd0UZZmi06dS4ZZSSyZTEpc1Fn4jSNMGuCIkMN3dBp10HM/qCuKEKhIBUZdHS0RvHHTNTqe398z92+q5fee7/ne/ne78fnI7n5nvM5n/M97096efG553vO+aaqkCS165cmXYAkaXkZ9JLUOINekhpn0EtS4wx6SWqcQS9JjTPoJalxBr0kNc6gl6TGrZ50AQBnnnlmbdiwYdJlSNJUufvuu79VVTML9VsRQb9hwwb2798/6TIkaaok+Y/F9PPUjSQ1zqCXpMYZ9JLUOINekhpn0EtS4xYM+iQ3Jjma5MBQ23uTPJjk3iSfTbJmaNs1SQ4leSjJa5arcEnS4ixmRv8x4OIT2vYAL6mqlwJfB64BSHIesAX49W6fv0uyamzVSpKWbMGgr6ovAd8+oe2LVXW8W70LWN8tbwZurqofVdU3gUPABWOsV5K0ROM4R/8W4PPd8jrg8aFth7s2SdKE9LozNsm1wHHgprmmebrN++3jSbYB2wDOPffcPmVIy2rD9ttH3vfRHZeNsRJpNCPP6JNsBV4LvKGq5sL8MHDOULf1wBPz7V9VO6tqtqpmZ2YWfFSDJGlEIwV9kouBq4HLq+qHQ5t2A1uSnJZkI7AJ+HL/MiVJo1rw1E2STwKvBM5Mchi4jsFVNqcBe5IA3FVVf1hV9ye5BXiAwSmdt1bVT5ereGkx+px6kVqwY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8ObdoNbElyWpKNwCbgy5OosadpHEeLj/HYDWztlrcCt02wlkVJEuAG4GBVvW9o0zSOZWbuirokzwJezeAzhzuA13Xdlmcsk/4kuscn2J8GDgD3Av8IrBv6ZPtDDM593cfQ1R8r9YfBh5OPA/d0Px8Z2nZtN5aHgEsmXesC4/g9BjPhHwFPAl+YxnEM1Xwpg6s8vgFcO+l6llj7J4EjwE+6f5OrgOcDe4GHu9czJl3nIsbxOwxOZdw79N/HpVM6lpcCX+3GcgB4d9f+QgYTn0PAPwCnjfvY3hkrSY2b2lM3kqTFMeglqXEGvSQ1zqCXpMYZ9JLUOINekhpn0EtS4wx6SWrc/wDYbEtnSxigAgAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "plt.hist(ar, bins = np.arange(-30,31,3)) " ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.75399945467625" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inspecting array\n", "- ar.size | Returns number of elements in array\n", "- ar.shape | Returns dimensions of array (rows,columns)\n", "- ar.dtype | Returns type of elements in array\n", "- ar.astype(dtype) | Convert arr elements to type dtype\n", "\n", "[Back to top](#back-to-top)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [0, 7]])" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar = np.random.randint(0,10, size=(2,2))\n", "ar" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.size" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.shape" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.ndim" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.dtype" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2., 1.],\n", " [0., 7.]])" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar.astype('float')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Copying/sorting/reshaping \n", "- np.copy(ar) \t| copies to *new* memory\n", "- ar.sort() \t| sort\n", "- ar.sort(axis=0) \n", "- two_d_ar.flatten() \t| Flattens 2D aray to 1D\n", "- ar.T \t| Transposes ar (rows become columns and vice versa)\n", "- ar.reshape(3,4) \t| Reshapes ar to 3 rows, 4 columns without changing data\n", "- ar.resize((5,6)) \t| Changes ar shape to 5x6 and fills new values with 0\n", "\n", "- ar.view(dtype) \t| Creates view of ar elements with type dtype" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [0, 7]])" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [0, 7]])" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = ar.copy()\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### shallow copy" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1000, 1],\n", " [ 0, 7]])" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c =ar\n", "c" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1000, 1],\n", " [ 0, 7]])" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar[0,0]=1000\n", "ar" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1000, 1],\n", " [ 0, 7]])" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# c is changed as it is a shallow copy of ar\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### deep copy (note this would have been a shallow copy if ar is a python list and not a numpy array)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "b = ar.copy()" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [0, 7]])" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar[0,0] = 0\n", "ar" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1000, 1],\n", " [ 0, 7]])" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [0, 7]])" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note that shallow copy acts as if it is the original except that it's got anther name. \n", "*Changing the shallow copy will change the original array*" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 999],\n", " [ 0, 7]])" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c[0,1] = 999\n", "c" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 999],\n", " [ 0, 7]])" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar #note that its [0,1] element is also changed!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a.sort(axis=-1, kind='quicksort', order=None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combining/splitting \n", "- np.concatenate((ar1,ar2),axis=0) | Adds ar2 as rows to the end of ar1\n", "- np.concatenate((ar1,ar2),axis=1) | Adds ar2 as columns to end of ar1\n", "- np.split(ar,3) | Splits ar into 3 sub-arays\n", "- np.hsplit(ar,5) | Splits ar horizontally on the 5th index\n", "\n", "[Back to top](#back-to-top)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 10, 10, 10, 10, 10, 10],\n", " [ 8, 6, 8, 6, 8, 6, 8, 6],\n", " [10, 10, 10, 10, 10, 10, 10, 10],\n", " [ 8, 6, 8, 6, 8, 6, 8, 6],\n", " [10, 10, 10, 10, 10, 10, 10, 10],\n", " [ 8, 6, 8, 6, 8, 6, 8, 6],\n", " [10, 10, 10, 10, 10, 10, 10, 10],\n", " [ 8, 6, 8, 6, 8, 6, 8, 6]])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dr = np.concatenate((ar,ar,ar,ar),axis=0)\n", "np.concatenate((dr,dr,dr,dr), axis=1)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([[10, 10]]), array([[8, 6]])]" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.split(ar,2)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([[10],\n", " [ 8]]), array([[10],\n", " [ 6]])]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hsplit(ar,2)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10],\n", " [ 8, 6]])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.concatenate(np.hsplit(ar,2), axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding/removing Elements \n", "- np.append(arr,values) | Appends values to end of array\n", "- np.insert(arr,2,values) | Inserts values into arr before index 2\n", "- np.delete(arr,3,axis=0) | Deletes row on index 3 of arr\n", "- np.delete(arr,4,axis=1) | Deletes column on index 4 of arr\n", "\n", "[Back to top](#back-to-top)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10],\n", " [ 8, 6]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 10, 8, 6, 5, 5])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#if you don't specify axis, then the result will be flattened\n", "np.append(ar,[5,5])" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10],\n", " [ 8, 6],\n", " [ 5, 5],\n", " [ 5, 5]])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#if you specify axis, then you must provide exactly the same shape of array(s)\n", "np.append(ar,[[5,5],[5,5]], axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Tip: If you don't specify axis, then the result from `np.append` will be flattened.\n", "\n", "If you specify axis, then you must provide exactly the same shape of array(s)\n", "
" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 5, 5],\n", " [ 8, 6, 5, 5]])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.append(ar,[[5,5],[5,5]], axis=1)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [2, 2],\n", " [3, 3]])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1, 1], [2, 2], [3, 3]])\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Tip: np.insert(arr, obj, values, axis=None)\n", "obj=: Object that defines the index or indices before which `values` is inserted.\n", "
" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 1, 1, 2, 2, 3, 3])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#np.insert(arr, obj, values, axis=None)\n", "# obj=: Object that defines the index or indices before which `values` is inserted.\n", "# in this example, 0 means the 0th index\n", "np.insert(a, 0, 5)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 2, 2, 3, 5, 3])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.insert(a, -1, 5)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3]).shape" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 1)" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1],[2],[3]]).shape" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [2, 2, 2],\n", " [3, 3, 3]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# in this example, [0] means to insert the 1D array as the very first column\n", "np.insert(a, [0],[[1],[2],[3]], axis=1)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [2, 2, 2],\n", " [3, 3, 3]])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# in this example, [1] means to insert the 1D array as the second column\n", "np.insert(a, [1], [[1],[2],[3]], axis=1)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [2, 2],\n", " [3, 3]])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 2, 2, 3, 3])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a.flatten()\n", "b" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 2, 2, 3, 3])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 5, 2, 6, 2, 3, 3])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.insert(b, slice(2, 4), [5, 6])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(8)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3],\n", " [4, 5, 6, 7]])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(8).reshape(2, 4)\n", "x" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 999, 1, 2, 999, 3],\n", " [ 4, 999, 5, 6, 999, 7]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = (1, 3) #this is row No. 2, column No. 3\n", "np.insert(x, idx, 999, axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Back to top](#back-to-top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing/slicing/subsetting\n", "- ar[5] | Returns the element at index 5\n", "- ar[2,5] | Returns the 2D aray element on index [2][5]\n", "- ar[1]=4 | Assigns aray element on index 1 the value 4\n", "- ar[1,3]=10 | Assigns aray element on index [1][3] the value 10\n", "- ar[0:3] | Returns the elements at indices 0,1,2 (On a 2D aray: returns rows 0,1,2)\n", "- ar[0:3,4] | Returns the elements on rows 0,1,2 at column 4\n", "- ar[:2] | Returns the elements at indices 0,1 (On a 2D aray: returns rows 0,1)\n", "- ar[:,1] | Returns the elements at index 1 on all rows\n", "- ar\\\\<5 | Returns an aray with boolean values\n", "- (ar1\\\\<3) & (ar2\\\\>5) | Returns an aray with boolean values\n", "- ~ar | Inverts a boolean aray\n", "- ar[ar<5] | Returns aray elements smaller than 5\n", "\n", "\n", "[Back to top](#back-to-top)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More examples " ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#array of 10 zeros \n", "np.zeros(10, dtype=int)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0.],\n", " [0., 0., 0.]])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((2,3),dtype=float)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.full((10,10),1)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((10,10), dtype=int)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## broadcasting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ex. 1 long - wide\n", "- long duplicates itself horizontally to match wide's width\n", "- wide duplicates itself vertically to match long's width" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [1]])" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.ones((2,1), dtype=int)\n", "X" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1]])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = np.ones((1,2), dtype=int)\n", "Y" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0],\n", " [0, 0]])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X - Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ex. 2 long - wide\n", "- long duplicates itself horizontally to match wide's width\n", "- wide duplicates itself vertically to match long's width" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2],\n", " [2]])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.full((2,1),2)\n", "X" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1]])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = np.full((1,2),1)\n", "Y" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [1, 1]])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X - Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ex. 3 long - wide\n", "- long duplicates itself horizontally to match wide's width\n", "- wide duplicates itself vertically to match long's width" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2]])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np. array([[1],[2]])\n", "X" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1]])" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = np.array([[2, 1]])\n", "Y" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1, 0],\n", " [ 0, 1]])" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X -Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tricking it into doing something with evey row\n", "If we think of each row is a point on 2-D space (like a sheet of paper), if we want to get its distance from all other points, including itself,which we called X here,\n", "\n", "then we reshape a copy of it into 3-D space, which we call Y. So when we take the difference between them, X will be duplicated along the 3rd dimension.\n", "\n", "The trick is that we do not reshape Y in (2,2,1). Rather, we reshape Y in (2,1,2). \n", "\n", "In the first 2D space, X is (2,2) whereas Y is (2,1). So Y has to duplicate itself to become (2,2). \n", "\n", "In the last dimension, X has to duplicate itself for Y. " ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0],\n", " [2, 1]])" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.array([[1,0],\n", " [2,1]])\n", "X" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 0]],\n", "\n", " [[2, 1]]])" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = X.reshape(2,1,2)\n", "Y" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 0],\n", " [-1, -1]],\n", "\n", " [[ 1, 1],\n", " [ 0, 0]]])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#[[0,0] ,[-1,-1]] = [[1, 0]] - [[1, 0],[2, 1]]\n", "#[[ 1, 1],[ 0, 0]]] = [[2, 1]] - [[1, 0],[2, 1]]\n", "Y-X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Let's check to see if we can replicate what numpy did" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0],\n", " [-1, -1]])" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1, 0]]) - np.array([[1, 0],[2, 1]])" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [0, 0]])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[2, 1]]) - np.array([[1, 0],[2, 1]])" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([array([[-1, -1],\n", " [-2, -2]]), array([[2, 1]]),\n", " array([[-2, -2],\n", " [-3, -3]]), array([[1, 0]])], dtype=object)" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack((np.array([[1, 0]]) - np.array([[1, 0],[2, 1]]), np.array([[2, 1]])) - np.array([[1, 0],[2, 1]]) )" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }