Created
November 10, 2024 09:35
-
-
Save devops-school/3ec92ead254a9c2d9c7c9a40682bcf5b to your computer and use it in GitHub Desktop.
Jupyter notebook – Lab Session – 11 – Numpy Introduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "aaff41b9", | |
"metadata": {}, | |
"source": [ | |
"# Numpy Introduction" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b87e66c8", | |
"metadata": {}, | |
"source": [ | |
"## Installing Numpy\n", | |
"## How To import NumPy\n", | |
"## Why use NumPy" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c33bf5a4", | |
"metadata": {}, | |
"source": [ | |
"# Terminology" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "32691996", | |
"metadata": {}, | |
"source": [ | |
"## What is an array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "56b12e52", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"a = np.array([[1, 2, 3, 4], [5, 6, 7 , 8], [9, 10, 11, 12]])\n", | |
"\n", | |
"print(a[0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "88708177", | |
"metadata": {}, | |
"source": [ | |
"## What are the attributes of an array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "cc10bf51", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"[[0., 0., 0.],\n", | |
"[1., 1., 1.]]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "658f2df1", | |
"metadata": {}, | |
"source": [ | |
"## Whats the difference between a Python list and a NumPy array?" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8555b23e", | |
"metadata": {}, | |
"source": [ | |
"### Creating Array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9418c5c9", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"np.array()\n", | |
"np.zeros()\n", | |
"np.ones()\n", | |
"np.empty()\n", | |
"np.arange()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "5d355575", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1, 2, 3])" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"a = np.array([1, 2, 3])\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "e97659f3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0., 0., 0., 0., 0., 0.])" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.zeros(6)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "23c45096", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1., 1., 1., 1., 1., 1.])" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.ones(6)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "435fea4e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1., 1., 1., 1., 1., 1.])" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Or even an empty array!\n", | |
"# The function empty creates an array whose initial content is random and depends on the state of the memory\n", | |
"\n", | |
"np.empty(6)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "72f4c92a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 1 2 3]\n", | |
"[0 2 4 6 8]\n" | |
] | |
} | |
], | |
"source": [ | |
"# you can create an array with a range of elements\n", | |
"\n", | |
"print(np.arange(4))\n", | |
"print(np.arange(0,10,2)) #(start, stop, step)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "e77d2fcb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 2, 7, 12, 17, 22, 27])" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.arange(2,29,5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "727878f7", | |
"metadata": {}, | |
"source": [ | |
"## Add, Remove, and Sort" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9406c6ee", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"np.append()\n", | |
"np.delete()\n", | |
"np.sort()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "7d173ff1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arr = np.array([1,2,3,4,5,6,7,8])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "76bf0ac1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1, 2, 3, 4, 5, 6, 7, 8, 1, 2])" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.append(arr, [1,2])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "85c5cc86", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1, 3, 4, 5, 6, 7, 8])" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.delete(arr, 1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "08231037", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1, 2, 3, 4, 5, 6, 7, 8])" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.sort(arr)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9e8dba62", | |
"metadata": {}, | |
"source": [ | |
"## Shape and Size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "bcd78629", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ndarray.ndim() #jumlah axes, dimensi dr array\n", | |
"ndarray.size() #jumlah total elemen array\n", | |
"ndarray.shape() #menampilkan tuple integer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "255df074", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[[0 1 2 3]\n", | |
" [4 5 6 7]]\n", | |
"\n", | |
" [[0 1 2 3]\n", | |
" [4 5 6 7]]\n", | |
"\n", | |
" [[0 1 2 3]\n", | |
" [4 5 6 7]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"array_example = np.array([[[0, 1, 2, 3],\n", | |
" [4, 5, 6, 7]],\n", | |
" \n", | |
" [[0, 1, 2, 3],\n", | |
" [4, 5, 6, 7]],\n", | |
" \n", | |
" [[0, 1, 2, 3],\n", | |
" [4, 5, 6, 7]]])\n", | |
"\n", | |
"print(array_example)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "387f348c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array_example.ndim" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "29ff39e0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"24" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array_example.size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "3943b6fe", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(3, 2, 4)" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array_example.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "a73feeee", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arr_one = np.array([[1, 2, 3, 4, 5]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"id": "aca578cb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arr_one.ndim" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"id": "44959b48", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arr_one.size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "398962b6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 5)" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arr_one.shape" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1849e73e", | |
"metadata": {}, | |
"source": [ | |
"## Reshape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "3a3839d0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 1 2 3 4 5]\n" | |
] | |
} | |
], | |
"source": [ | |
"a = np.arange(6)\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "4ce3f29f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0 1]\n", | |
" [2 3]\n", | |
" [4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"b = a.reshape(3,2)\n", | |
"print(b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "58fd04bb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0],\n", | |
" [1],\n", | |
" [2],\n", | |
" [3],\n", | |
" [4],\n", | |
" [5]])" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a.reshape(6,1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "afb24ad4", | |
"metadata": {}, | |
"source": [ | |
"## Convert 1D to 2D" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "14ea0887", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"np.newaxis\n", | |
"np.expand_dims" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"id": "000c153f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(6,)" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = np.array([1, 2, 3, 4, 5, 6])\n", | |
"a.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "c1acf582", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 6)\n", | |
"[[1 2 3 4 5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# you can use np.newaxis to add a new axis:\n", | |
"\n", | |
"a2 = a[np.newaxis]\n", | |
"print(a2.shape)\n", | |
"print(a2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"id": "17650b9b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 6)\n", | |
"[[1 2 3 4 5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# you can convert a 1d array to a row vector by interesting an axis along the first dimension\n", | |
"\n", | |
"row_vector = a[np.newaxis, :]\n", | |
"print(row_vector.shape)\n", | |
"print(row_vector)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"id": "f4e58a25", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(6, 1)\n", | |
"[[1]\n", | |
" [2]\n", | |
" [3]\n", | |
" [4]\n", | |
" [5]\n", | |
" [6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# for a column vector, you can insert an axis along the second dimension\n", | |
"\n", | |
"col_vector = a[:, np.newaxis]\n", | |
"print(col_vector.shape)\n", | |
"print(col_vector)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "28a7e3c1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(6,)" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = np.array([1, 2, 3, 4, 5, 6])\n", | |
"a.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "c8ea8a31", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(6, 1)" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# you can use np.expand_dims to add an axis at index position 1 with:\n", | |
"\n", | |
"b = np.expand_dims(a, axis=1)\n", | |
"b.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "4a8366fd", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1, 6)" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# you can add an axis at index position 0 with:\n", | |
"\n", | |
"c = np.expand_dims(a, axis=0)\n", | |
"c.shape" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3280fc5d", | |
"metadata": {}, | |
"source": [ | |
"# Indexing and Slicing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "ff43d6df", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3]\n", | |
"1\n", | |
"2\n", | |
"[1 2]\n", | |
"[2 3]\n", | |
"[2 3]\n" | |
] | |
} | |
], | |
"source": [ | |
"data = np.array([1, 2, 3])\n", | |
"\n", | |
"print(data)\n", | |
"print(data[0])\n", | |
"print(data[1])\n", | |
"print(data[0:2])\n", | |
"print(data[1:])\n", | |
"print(data[-2:])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"id": "01ff8488", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 1, 2, 3, 4],\n", | |
" [ 5, 6, 7, 8],\n", | |
" [ 9, 10, 11, 12]])" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"id": "c3a0c26e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 5 6 7 8 9 10 11 12]\n" | |
] | |
} | |
], | |
"source": [ | |
"#you can easily print all of the values in the array that are more than 5\n", | |
"print(a[a>=5])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "c83c56f4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 5 6 7 8 9 10 11 12]\n", | |
"[ 5 6 7 8 9 10 11 12]\n" | |
] | |
} | |
], | |
"source": [ | |
"five_up = (a >= 5)\n", | |
"\n", | |
"print(a[five_up])\n", | |
"print(a[a>=5])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "f1c60abe", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 2 4 6 8 10 12]\n" | |
] | |
} | |
], | |
"source": [ | |
"# you can select elements that are divisible by 2\n", | |
"\n", | |
"divisible_by_2 = a[a%2==0]\n", | |
"print(divisible_by_2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "d9042779", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 3 4 5 6 7 8 9 10]\n" | |
] | |
} | |
], | |
"source": [ | |
"# you can select elements that satisfy two conditions using the & and | operators\n", | |
"\n", | |
"c = a[(a > 2) & (a < 11)]\n", | |
"\n", | |
"print(c)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7d3fd494", | |
"metadata": {}, | |
"source": [ | |
"# Creating Array from Existing Data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "995335b3", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"slicing indexing\n", | |
"np.vstack()\n", | |
"np.hstack()\n", | |
"np.hsplit()\n", | |
".view()\n", | |
".copy()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "f0e0c97c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "288b9209", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([4, 5, 6, 7, 8])" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arr1 = arr[3:8]\n", | |
"arr1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "2f960253", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a_1 = np.array([[1, 1],\n", | |
" [2, 2]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "fa52de6c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a_2 = np.array([[3, 3],\n", | |
" [4, 4]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "3eb0643f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1, 1],\n", | |
" [2, 2],\n", | |
" [3, 3],\n", | |
" [4, 4]])" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.vstack((a_1, a_2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "aa61f5a3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1, 1, 3, 3],\n", | |
" [2, 2, 4, 4]])" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# or stack them horizontally with hstack\n", | |
"\n", | |
"np.hstack((a_1, a_2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"id": "cc7dbdb3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 1 2 3 4 5 6 7 8 9 10 11 12]\n", | |
" [13 14 15 16 17 18 19 20 21 22 23 24]]\n" | |
] | |
} | |
], | |
"source": [ | |
"arrsplit = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", | |
" [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]])\n", | |
"\n", | |
"print(arrsplit)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "74520b62", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[array([[ 1, 2, 3, 4],\n", | |
" [13, 14, 15, 16]]),\n", | |
" array([[ 5, 6, 7, 8],\n", | |
" [17, 18, 19, 20]]),\n", | |
" array([[ 9, 10, 11, 12],\n", | |
" [21, 22, 23, 24]])]" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# if u wanted to split this array into three equally shaped arrays\n", | |
"\n", | |
"np.hsplit(arrsplit, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "ec857d34", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 1, 2, 3, 4],\n", | |
" [ 5, 6, 7, 8],\n", | |
" [ 9, 10, 11, 12]])" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n", | |
"a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"id": "5f85845d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 1, 2, 3, 4],\n", | |
" [ 5, 6, 7, 8],\n", | |
" [ 9, 10, 11, 12]])" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# u can create a new array object that looks at the same data\n", | |
"\n", | |
"b = a.view()\n", | |
"b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "7954549a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 1, 2, 3, 4],\n", | |
" [ 5, 6, 7, 8],\n", | |
" [ 9, 10, 11, 12]])" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# using the copy method will make a complete copy of the array and its data (a deep copy)\n", | |
"\n", | |
"c = a.copy()\n", | |
"c" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "09316b8a", | |
"metadata": {}, | |
"source": [ | |
"# Basic array operations" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"id": "590b5881", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Addition, Subtraction, Multiplication, Division, and More.." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"id": "337331d4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = np.array([1, 2, 3, 4])\n", | |
"\n", | |
"# add all of the elements in the array\n", | |
"a.sum()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"id": "91278025", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1, 1],\n", | |
" [2, 2]])" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"b = np.array([[1, 1], [2, 2]])\n", | |
"b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"id": "c3586ccf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([3, 3])" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# u can sum the rows\n", | |
"b.sum(axis=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"id": "80b3f077", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([2, 4])" | |
] | |
}, | |
"execution_count": 54, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# u can sum the columns\n", | |
"b.sum(axis=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"id": "21857beb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1, 2])" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data = np.array([1, 2])\n", | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"id": "d0e59255", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1., 1.])" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ones = np.ones(2)\n", | |
"ones" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"id": "fd78a236", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([2., 3.])" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data + ones" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"id": "5102b9d4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1, 4])" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data * data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"id": "d5d15a26", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([1., 1.])" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data / data" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "72b7ff21", | |
"metadata": {}, | |
"source": [ | |
"# Broadcasting" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"id": "a7719c79", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([2, 4])" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data * 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5a1256f5", | |
"metadata": {}, | |
"source": [ | |
"# More Array Operations" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"id": "b953600a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Maximum, minimum, sum, mean, product, standar deviation, and more.." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"id": "4c4dcd4f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data.max()\n", | |
"data.min()\n", | |
"data.sum()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8c010b4c", | |
"metadata": {}, | |
"source": [ | |
"# Matrices" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f9e85cc8", | |
"metadata": {}, | |
"source": [ | |
"## Creating Matrices" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"id": "9f142675", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0.49292191, 0.1593913 ],\n", | |
" [0.43512884, 0.26043418],\n", | |
" [0.69031586, 0.26080554]])" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.ones((3,2))\n", | |
"np.zeros((3,2))\n", | |
"np.random.random((3,2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"id": "d1bfa2bc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1. 1.]\n", | |
" [1. 1.]\n", | |
" [1. 1.]]\n", | |
"[[0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]]\n", | |
"[[0.05451766 0.09732209]\n", | |
" [0.73913936 0.98199571]\n", | |
" [0.95590904 0.83658468]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.ones((3,2)))\n", | |
"print(np.zeros((3, 2)))\n", | |
"print(np.random.random((3,2)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4d706525", | |
"metadata": {}, | |
"source": [ | |
"## Matrix Arithmetic" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"id": "4234e095", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]]\n" | |
] | |
} | |
], | |
"source": [ | |
"data = np.array([[1,2], [3,4]])\n", | |
"print(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"id": "2d7d2a47", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1. 1.]\n", | |
" [1. 1.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"ones = np.ones([2, 2])\n", | |
"print(ones)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"id": "0ea72500", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[2. 3.]\n", | |
" [4. 5.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data + ones)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"id": "be158ec2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1. 1.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"ones_row = np.ones([1,2])\n", | |
"print(ones_row)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"id": "f788b7b2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[2. 3.]\n", | |
" [4. 5.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data + ones_row)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3c1caf5f", | |
"metadata": {}, | |
"source": [ | |
"# Dot Product" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"id": "c572160a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2 3]\n", | |
" [4 5 6]]\n", | |
"(2, 3)\n", | |
"[[ 7 8]\n", | |
" [ 9 10]\n", | |
" [11 12]]\n", | |
"(3, 2)\n" | |
] | |
} | |
], | |
"source": [ | |
"a_1 = np.array([[1,2,3], [4,5,6]])\n", | |
"print(a_1)\n", | |
"print(a_1.shape)\n", | |
"\n", | |
"a_2 = np.array([[7,8], [9,10], [11,12]])\n", | |
"print(a_2)\n", | |
"print(a_2.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "9fdd7ff1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[ 58, 64],\n", | |
" [139, 154]])" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.dot(a_1, a_2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0c773ae1", | |
"metadata": {}, | |
"source": [ | |
"# Matrix Indexing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"id": "8a1a1c4c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]\n", | |
" [5 6]]\n", | |
"[1 2]\n", | |
"[3 4]\n", | |
"2\n", | |
"[[3 4]\n", | |
" [5 6]]\n", | |
"[2 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"data = np.array([[1,2], [3,4], [5,6]])\n", | |
"\n", | |
"print(data)\n", | |
"print(data[0])\n", | |
"print(data[1])\n", | |
"print(data[0,1])\n", | |
"print(data[1:3])\n", | |
"print(data[0:2,1])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "237cf1ab", | |
"metadata": {}, | |
"source": [ | |
"# Matrix Aggregation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"id": "6deb8b16", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]\n", | |
" [5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"id": "c686fb64", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"6\n", | |
"1\n", | |
"21\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data.max())\n", | |
"print(data.min())\n", | |
"print(data.sum())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"id": "c3fd9c93", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[5 6]\n", | |
"[2 4 6]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data.max(axis=0))\n", | |
"print(data.max(axis=1))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "64b8aa99", | |
"metadata": {}, | |
"source": [ | |
"# Transposing and Reshaping" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"id": "8a3afd0a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 2]\n", | |
" [3 4]\n", | |
" [5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"id": "fee15967", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 3 5]\n", | |
" [2 4 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(data.T)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"id": "27ccf4a5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1]\n", | |
" [2]\n", | |
" [3]\n", | |
" [4]\n", | |
" [5]\n", | |
" [6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"data_col = np.array([[1,2,3,4,5,6]]).T\n", | |
"print(data_col)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"id": "30d62c2f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1, 2, 3],\n", | |
" [4, 5, 6]])" | |
] | |
}, | |
"execution_count": 81, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data_col.reshape(2,3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"id": "16c0d3bc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1, 2],\n", | |
" [3, 4],\n", | |
" [5, 6]])" | |
] | |
}, | |
"execution_count": 82, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data_col.reshape(3,2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"id": "15ed9ae3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0 1 2]\n", | |
" [3 4 5]]\n" | |
] | |
} | |
], | |
"source": [ | |
"arr = np.arange(6).reshape((2,3))\n", | |
"print(arr)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"id": "6d285b71", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[[1 2]\n", | |
" [3 4]]\n", | |
"\n", | |
" [[5 6]\n", | |
" [7 8]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"ndarr = np.array([[[1, 2], [3,4]],\n", | |
" [[5, 6], [7, 8]]])\n", | |
"print(ndarr)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"id": "47c8cce4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[[1. 1.]\n", | |
" [1. 1.]\n", | |
" [1. 1.]]\n", | |
"\n", | |
" [[1. 1.]\n", | |
" [1. 1.]\n", | |
" [1. 1.]]\n", | |
"\n", | |
" [[1. 1.]\n", | |
" [1. 1.]\n", | |
" [1. 1.]]\n", | |
"\n", | |
" [[1. 1.]\n", | |
" [1. 1.]\n", | |
" [1. 1.]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.ones((4,3,2)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"id": "1273244e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[[0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]]\n", | |
"\n", | |
" [[0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]]\n", | |
"\n", | |
" [[0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]]\n", | |
"\n", | |
" [[0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.zeros((4,3,2)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"id": "cf8167e9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[[0.79568529 0.70048239]\n", | |
" [0.01458117 0.75937567]\n", | |
" [0.85644883 0.67510868]]\n", | |
"\n", | |
" [[0.82429833 0.84009191]\n", | |
" [0.35086451 0.54936671]\n", | |
" [0.99348265 0.7521005 ]]\n", | |
"\n", | |
" [[0.07473488 0.77458928]\n", | |
" [0.22606195 0.18627498]\n", | |
" [0.05133246 0.80805047]]\n", | |
"\n", | |
" [[0.3770231 0.14832845]\n", | |
" [0.63940574 0.88564686]\n", | |
" [0.77236064 0.18111657]]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.random.random((4,3,2)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b9cf6c1c", | |
"metadata": {}, | |
"source": [ | |
"# Flatten N-Dimensional Array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"id": "ec926afb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 1 2 3 4]\n", | |
" [ 5 6 7 8]\n", | |
" [ 9 10 11 12]]\n" | |
] | |
} | |
], | |
"source": [ | |
"arrflat = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", | |
"print(arrflat)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"id": "100f5989", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])" | |
] | |
}, | |
"execution_count": 89, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# u can use flatten to flatten ur array into 1d array\n", | |
"\n", | |
"arrflat.flatten()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0000188d", | |
"metadata": {}, | |
"source": [ | |
"# Working with Math Formulas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"id": "e8d3ba68", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'n' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_15384/3104529244.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0merror\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobserved\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mprediction\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;31mNameError\u001b[0m: name 'n' is not defined" | |
] | |
} | |
], | |
"source": [ | |
"error = (1/n) * np.sum(np.square(observed - prediction))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "7827f49e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.8.8" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment