"
]
},
"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
}