Laboratoria 1 - poprawki i uzupełnienia

This commit is contained in:
Paweł Skórzewski 2022-03-12 10:10:59 +01:00
parent 1f5c1d96d8
commit 8df2896694
2 changed files with 151 additions and 68 deletions

View File

@ -26,6 +26,44 @@
"### Listy składane (*List comprehension*)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]\n"
]
}
],
"source": [
"lista = [i**2 for i in range(50)]\n",
"\n",
"print(lista)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}\n"
]
}
],
"source": [
"slownik = {k: k**2 for k in range(10) if k % 2 == 0}\n",
"\n",
"print(slownik)"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -251,7 +289,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -280,7 +318,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@ -289,7 +327,7 @@
"(3, 3)"
]
},
"execution_count": 15,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@ -300,7 +338,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@ -309,7 +347,7 @@
"array([12, 15, 18])"
]
},
"execution_count": 16,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@ -320,22 +358,22 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2., 5., 8.])"
"array([1, 4, 7])"
]
},
"execution_count": 17,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.mean(axis=1) # wektor złożony ze średnich elementów w każdym wierszu"
"x.min(axis=1) # wektor złożony ze średnich elementów w każdym wierszu"
]
},
{
@ -347,7 +385,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@ -356,7 +394,27 @@
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 18,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(list(range(10)))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@ -367,7 +425,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@ -376,7 +434,7 @@
"array([5, 6, 7, 8, 9])"
]
},
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@ -387,22 +445,22 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])"
"array([5, 7, 9])"
]
},
"execution_count": 20,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(5, 10, 0.5)"
"np.arange(5, 10, 2)"
]
},
{
@ -414,7 +472,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 18,
"metadata": {
"scrolled": true
},
@ -446,19 +504,20 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 1.25 2.5 3.75 5. ]\n"
"[0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556\n",
" 0.66666667 0.77777778 0.88888889 1. ]\n"
]
}
],
"source": [
"x = np.linspace(0, 5, 5)\n",
"x = np.linspace(0, 1, 10)\n",
"print(x)"
]
},
@ -1036,7 +1095,7 @@
}
],
"source": [
"a[:10:2]"
"a[:10:2] # elementy do 10., co drugi element"
]
},
{
@ -1056,7 +1115,7 @@
}
],
"source": [
"a[::-1]"
"a[::-1] # wszytkie elementy tablicy `a`, ale w odwróconej kolejności"
]
},
{
@ -1068,7 +1127,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@ -1079,7 +1138,7 @@
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 46,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@ -1091,22 +1150,11 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"x[2, 3]"
"x[2, 3] # wiersz 2, kolumna 3"
]
},
{
@ -1126,7 +1174,7 @@
}
],
"source": [
"x[:, 1]"
"x[:, 1] # kolumna 1"
]
},
{
@ -1146,7 +1194,7 @@
}
],
"source": [
"x[1, :]"
"x[1, :] # wiersz 1"
]
},
{
@ -1167,7 +1215,7 @@
}
],
"source": [
"x[1:3, :]"
"x[1:3, :] # wiersze od 1. włącznie do 3. wyłącznie"
]
},
{
@ -1337,6 +1385,7 @@
}
],
"source": [
"# Operacja `.flat` \"spłaszcza\" macierz\n",
"for element in x.flat:\n",
" print(element) "
]
@ -1365,7 +1414,7 @@
}
],
"source": [
"np.random.randint(0, 10, 5)"
"np.random.randint(0, 10, 5) # Tablica złożona z 5 liczb całkowitych wylosowanych z zakresu od 0 do 10"
]
},
{
@ -1385,7 +1434,7 @@
}
],
"source": [
"np.random.normal(0, 1, 5) "
"np.random.normal(0, 1, 5) # Tablica złożona z 5 liczb wylosowanych z rozkładu normalnego (0, 1)"
]
},
{
@ -1405,7 +1454,7 @@
}
],
"source": [
"np.random.uniform(0, 2, 5)"
"np.random.uniform(0, 2, 5) # Tablica złożona z 5 liczb wylosowanych z rozkładu jednostajnego na przedziale (0, 1)"
]
},
{
@ -1421,6 +1470,8 @@
"source": [
"NumPy jest pakietem wykorzystywanym do obliczeń w dziedzinie algebry liniowej, co jeszcze szczególnie przydatne w uczeniu maszynowym. \n",
"\n",
"**Transpozycja** to operacja zamiany wierszy na kolumny i na odwrót.\n",
"\n",
"Wektor o wymiarach $1 \\times N$ \n",
"$$\n",
" x =\n",
@ -1432,7 +1483,7 @@
" \\end{pmatrix} \n",
"$$\n",
"\n",
"i jego transpozycję $x^\\top = (x_{1}, x_{2},\\ldots,x_{N})$ można wyrazić w Pythonie w następujący sposób:"
"i jego transpozycję  $x^\\top = (x_{1}, x_{2},\\ldots,x_{N})$ można wyrazić w Pythonie w następujący sposób:"
]
},
{
@ -1702,7 +1753,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@ -1712,7 +1763,7 @@
" [ 5, 5]])"
]
},
"execution_count": 60,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@ -1724,7 +1775,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@ -1734,7 +1785,7 @@
" [ 0.5, 0.4]])"
]
},
"execution_count": 61,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@ -1744,6 +1795,26 @@
"invA"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.5 -0.2]\n",
" [ 0.5 0.4]]\n"
]
}
],
"source": [
"B = np.matrix(A)\n",
"invB = B**-1\n",
"print(invB)"
]
},
{
"cell_type": "code",
"execution_count": 62,
@ -2230,7 +2301,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.7.6"
},
"livereveal": {
"start_slideshow_at": "selected",

File diff suppressed because one or more lines are too long