Formatowanie notesów do lab. 1 i 2

This commit is contained in:
Paweł Skórzewski 2022-10-14 11:38:56 +02:00
parent 8f1331c439
commit e5c9192df3
2 changed files with 250 additions and 262 deletions

View File

@ -47,13 +47,13 @@
} }
], ],
"source": [ "source": [
"zdanie = 'tracz tarł tarcicę tak takt w takt jak takt w takt tarcicę tartak tarł'\n", "zdanie = \"tracz tarł tarcicę tak takt w takt jak takt w takt tarcicę tartak tarł\"\n",
"wyrazy = zdanie.split()\n", "wyrazy = zdanie.split()\n",
"dlugosci_wyrazow = []\n", "dlugosci_wyrazow = []\n",
"for wyraz in wyrazy:\n", "for wyraz in wyrazy:\n",
" dlugosci_wyrazow.append(len(wyraz))\n", " dlugosci_wyrazow.append(len(wyraz))\n",
" \n", "\n",
"print(dlugosci_wyrazow)" "print(dlugosci_wyrazow)\n"
] ]
}, },
{ {
@ -77,11 +77,11 @@
} }
], ],
"source": [ "source": [
"zdanie = 'tracz tarł tarcicę tak takt w takt jak takt w takt tarcicę tartak tarł'\n", "zdanie = \"tracz tarł tarcicę tak takt w takt jak takt w takt tarcicę tartak tarł\"\n",
"wyrazy = zdanie.split()\n", "wyrazy = zdanie.split()\n",
"dlugosci_wyrazow = [len(wyraz) for wyraz in wyrazy]\n", "dlugosci_wyrazow = [len(wyraz) for wyraz in wyrazy]\n",
"\n", "\n",
"print(dlugosci_wyrazow)" "print(dlugosci_wyrazow)\n"
] ]
}, },
{ {
@ -105,19 +105,19 @@
} }
], ],
"source": [ "source": [
"zdanie = 'tracz tarł tarcicę tak takt w takt jak takt w takt tarcicę tartak tarł'\n", "zdanie = \"tracz tarł tarcicę tak takt w takt jak takt w takt tarcicę tartak tarł\"\n",
"wyrazy = zdanie.split()\n", "wyrazy = zdanie.split()\n",
"\n", "\n",
"# Ta konstrukcja:\n", "# Ta konstrukcja:\n",
"dlugosci_wyrazow = []\n", "dlugosci_wyrazow = []\n",
"for wyraz in wyrazy:\n", "for wyraz in wyrazy:\n",
" if wyraz != 'takt':\n", " if wyraz != \"takt\":\n",
" dlugosci_wyrazow.append(wyraz)\n", " dlugosci_wyrazow.append(wyraz)\n",
" \n",
"# ...jest równoważna tej jednolinijkowej:\n",
"dlugosci_wyrazow = [len(wyraz) for wyraz in wyrazy if wyraz != 'takt']\n",
"\n", "\n",
"print(dlugosci_wyrazow)" "# ...jest równoważna tej jednolinijkowej:\n",
"dlugosci_wyrazow = [len(wyraz) for wyraz in wyrazy if wyraz != \"takt\"]\n",
"\n",
"print(dlugosci_wyrazow)\n"
] ]
}, },
{ {
@ -149,9 +149,9 @@
} }
], ],
"source": [ "source": [
"napis = 'abcde'\n", "napis = \"abcde\"\n",
"print(napis[0]) # 'a'\n", "print(napis[0]) # 'a'\n",
"print(napis[4]) # 'e'" "print(napis[4]) # 'e'\n"
] ]
}, },
{ {
@ -177,10 +177,10 @@
} }
], ],
"source": [ "source": [
"napis = 'abcde'\n", "napis = \"abcde\"\n",
"print(napis[-1]) # 'e' („ostatni”)\n", "print(napis[-1]) # 'e' („ostatni”)\n",
"print(napis[-2]) # 'd' („drugi od końca”)\n", "print(napis[-2]) # 'd' („drugi od końca”)\n",
"print(napis[-5]) # 'a' („piąty od końca”)" "print(napis[-5]) # 'a' („piąty od końca”)\n"
] ]
}, },
{ {
@ -210,14 +210,18 @@
} }
], ],
"source": [ "source": [
"napis = 'abcde'\n", "napis = \"abcde\"\n",
"print(napis[1:4]) # 'bcd' („znaki od 1. włącznie do 4. wyłącznie”)\n", "print(napis[1:4]) # 'bcd' („znaki od 1. włącznie do 4. wyłącznie”)\n",
"print(napis[1:2]) # 'b' (to samo co `napis[1]`)\n", "print(napis[1:2]) # 'b' (to samo co `napis[1]`)\n",
"print(napis[-3:-1]) # 'cd' (kroić można też stosując indeksowanie od końca)\n", "print(napis[-3:-1]) # 'cd' (kroić można też stosując indeksowanie od końca)\n",
"print(napis[1:-1]) # 'bcd' (możemy nawet mieszać te dwa sposoby indeksowania)\n", "print(napis[1:-1]) # 'bcd' (możemy nawet mieszać te dwa sposoby indeksowania)\n",
"print(napis[3:]) # 'de' (jeżeli koniec przedziału nie jest podany, to kroimy do samego końca łańcucha)\n", "print(\n",
"print(napis[:3]) # 'abc' (jeżeli początek przedziału nie jest podany, to kroimy od początku łańcucha)\n", " napis[3:]\n",
"print(napis[:]) # 'abcde' (kopia całego napisu)" ") # 'de' (jeżeli koniec przedziału nie jest podany, to kroimy do samego końca łańcucha)\n",
"print(\n",
" napis[:3]\n",
") # 'abc' (jeżeli początek przedziału nie jest podany, to kroimy od początku łańcucha)\n",
"print(napis[:]) # 'abcde' (kopia całego napisu)\n"
] ]
}, },
{ {
@ -267,8 +271,8 @@
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"\n", "\n",
"x = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", "x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -295,7 +299,7 @@
} }
], ],
"source": [ "source": [
"x.shape" "x.shape\n"
] ]
}, },
{ {
@ -315,7 +319,7 @@
} }
], ],
"source": [ "source": [
"x.sum(axis=0)" "x.sum(axis=0)\n"
] ]
}, },
{ {
@ -335,7 +339,7 @@
} }
], ],
"source": [ "source": [
"x.mean(axis=1)" "x.mean(axis=1)\n"
] ]
}, },
{ {
@ -362,7 +366,7 @@
} }
], ],
"source": [ "source": [
"np.arange(10)" "np.arange(10)\n"
] ]
}, },
{ {
@ -382,7 +386,7 @@
} }
], ],
"source": [ "source": [
"np.arange(5, 10)" "np.arange(5, 10)\n"
] ]
}, },
{ {
@ -402,7 +406,7 @@
} }
], ],
"source": [ "source": [
"np.arange(5, 10, 0.5)" "np.arange(5, 10, 0.5)\n"
] ]
}, },
{ {
@ -434,7 +438,7 @@
"x = np.arange(1, 13)\n", "x = np.arange(1, 13)\n",
"print(x)\n", "print(x)\n",
"y = x.reshape(3, 4)\n", "y = x.reshape(3, 4)\n",
"print(y)" "print(y)\n"
] ]
}, },
{ {
@ -459,7 +463,7 @@
], ],
"source": [ "source": [
"x = np.linspace(0, 5, 5)\n", "x = np.linspace(0, 5, 5)\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -520,7 +524,7 @@
} }
], ],
"source": [ "source": [
"help(np.shape)" "help(np.shape)\n"
] ]
}, },
{ {
@ -552,8 +556,8 @@
"x = np.array([0.1, 0.2, 0.3])\n", "x = np.array([0.1, 0.2, 0.3])\n",
"print(x, \"- typ: \", x.dtype)\n", "print(x, \"- typ: \", x.dtype)\n",
"\n", "\n",
"x = np.array([1, 2, 3], dtype='float64')\n", "x = np.array([1, 2, 3], dtype=\"float64\")\n",
"print(x, \"- typ: \", x.dtype)" "print(x, \"- typ: \", x.dtype)\n"
] ]
}, },
{ {
@ -579,8 +583,8 @@
} }
], ],
"source": [ "source": [
"x = np.zeros([3,4])\n", "x = np.zeros([3, 4])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -599,8 +603,8 @@
} }
], ],
"source": [ "source": [
"x = np.ones([3,4])\n", "x = np.ones([3, 4])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -635,7 +639,7 @@
"\n", "\n",
"a = np.array([3, 4, 5])\n", "a = np.array([3, 4, 5])\n",
"b = np.ones(3)\n", "b = np.ones(3)\n",
"print(a - b)" "print(a - b)\n"
] ]
}, },
{ {
@ -661,7 +665,7 @@
], ],
"source": [ "source": [
"a = np.array([[1, 2], [3, 4]])\n", "a = np.array([[1, 2], [3, 4]])\n",
"print(a)" "print(a)\n"
] ]
}, },
{ {
@ -680,7 +684,7 @@
], ],
"source": [ "source": [
"b = np.array([[1, 2], [3, 4]])\n", "b = np.array([[1, 2], [3, 4]])\n",
"print(b)" "print(b)\n"
] ]
}, },
{ {
@ -701,7 +705,7 @@
} }
], ],
"source": [ "source": [
"a * b # mnożenie element po elemencie" "a * b # mnożenie element po elemencie\n"
] ]
}, },
{ {
@ -722,7 +726,7 @@
} }
], ],
"source": [ "source": [
"np.dot(a,b) # mnożenie macierzowe" "np.dot(a, b) # mnożenie macierzowe\n"
] ]
}, },
{ {
@ -743,7 +747,7 @@
} }
], ],
"source": [ "source": [
"np.matmul(a,b) # mnożenie macierzowe" "np.matmul(a, b) # mnożenie macierzowe\n"
] ]
}, },
{ {
@ -771,9 +775,9 @@
} }
], ],
"source": [ "source": [
"a = np.zeros((2, 2), dtype='float')\n", "a = np.zeros((2, 2), dtype=\"float\")\n",
"a += 5\n", "a += 5\n",
"a" "a\n"
] ]
}, },
{ {
@ -795,7 +799,7 @@
], ],
"source": [ "source": [
"a *= 5\n", "a *= 5\n",
"a" "a\n"
] ]
}, },
{ {
@ -816,7 +820,7 @@
} }
], ],
"source": [ "source": [
"a + a" "a + a\n"
] ]
}, },
{ {
@ -846,7 +850,7 @@
"a = np.array([1, 2, 3])\n", "a = np.array([1, 2, 3])\n",
"b = np.array([4, 5, 6])\n", "b = np.array([4, 5, 6])\n",
"c = np.array([7, 8, 9])\n", "c = np.array([7, 8, 9])\n",
"np.hstack([a, b, c])" "np.hstack([a, b, c])\n"
] ]
}, },
{ {
@ -870,7 +874,7 @@
} }
], ],
"source": [ "source": [
"np.vstack([a, b, c])" "np.vstack([a, b, c])\n"
] ]
}, },
{ {
@ -898,7 +902,7 @@
], ],
"source": [ "source": [
"x = np.arange(1, 5)\n", "x = np.arange(1, 5)\n",
"np.sqrt(x) * np.pi" "np.sqrt(x) * np.pi\n"
] ]
}, },
{ {
@ -918,7 +922,7 @@
} }
], ],
"source": [ "source": [
"2**4" "2**4\n"
] ]
}, },
{ {
@ -938,7 +942,7 @@
} }
], ],
"source": [ "source": [
"np.power(2, 4)" "np.power(2, 4)\n"
] ]
}, },
{ {
@ -958,7 +962,7 @@
} }
], ],
"source": [ "source": [
"np.log(np.e)" "np.log(np.e)\n"
] ]
}, },
{ {
@ -979,7 +983,7 @@
], ],
"source": [ "source": [
"x = np.arange(5)\n", "x = np.arange(5)\n",
"x.max() - x.min()" "x.max() - x.min()\n"
] ]
}, },
{ {
@ -1014,7 +1018,7 @@
], ],
"source": [ "source": [
"a = np.arange(10)\n", "a = np.arange(10)\n",
"a[2:4]" "a[2:4]\n"
] ]
}, },
{ {
@ -1034,7 +1038,7 @@
} }
], ],
"source": [ "source": [
"a[:10:2]" "a[:10:2]\n"
] ]
}, },
{ {
@ -1054,7 +1058,7 @@
} }
], ],
"source": [ "source": [
"a[::-1]" "a[::-1]\n"
] ]
}, },
{ {
@ -1084,7 +1088,7 @@
], ],
"source": [ "source": [
"x = np.arange(12).reshape(3, 4)\n", "x = np.arange(12).reshape(3, 4)\n",
"x" "x\n"
] ]
}, },
{ {
@ -1104,7 +1108,7 @@
} }
], ],
"source": [ "source": [
"x[2, 3]" "x[2, 3]\n"
] ]
}, },
{ {
@ -1124,7 +1128,7 @@
} }
], ],
"source": [ "source": [
"x[:, 1]" "x[:, 1]\n"
] ]
}, },
{ {
@ -1144,7 +1148,7 @@
} }
], ],
"source": [ "source": [
"x[1, :]" "x[1, :]\n"
] ]
}, },
{ {
@ -1165,7 +1169,7 @@
} }
], ],
"source": [ "source": [
"x[1:3, :]" "x[1:3, :]\n"
] ]
}, },
{ {
@ -1200,7 +1204,7 @@
], ],
"source": [ "source": [
"a = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3])\n", "a = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3])\n",
"a[a > 1]" "a[a > 1]\n"
] ]
}, },
{ {
@ -1220,7 +1224,7 @@
} }
], ],
"source": [ "source": [
"a[a == 3]" "a[a == 3]\n"
] ]
}, },
{ {
@ -1240,7 +1244,7 @@
} }
], ],
"source": [ "source": [
"np.where(a < 3)" "np.where(a < 3)\n"
] ]
}, },
{ {
@ -1260,7 +1264,7 @@
} }
], ],
"source": [ "source": [
"np.where(a < 3)[0]" "np.where(a < 3)[0]\n"
] ]
}, },
{ {
@ -1280,7 +1284,7 @@
} }
], ],
"source": [ "source": [
"np.where(a > 9)" "np.where(a > 9)\n"
] ]
}, },
{ {
@ -1307,7 +1311,7 @@
], ],
"source": [ "source": [
"for row in x:\n", "for row in x:\n",
" print(row)" " print(row)\n"
] ]
}, },
{ {
@ -1336,7 +1340,7 @@
], ],
"source": [ "source": [
"for element in x.flat:\n", "for element in x.flat:\n",
" print(element) " " print(element)\n"
] ]
}, },
{ {
@ -1363,7 +1367,7 @@
} }
], ],
"source": [ "source": [
"np.random.randint(0, 10, 5)" "np.random.randint(0, 10, 5)\n"
] ]
}, },
{ {
@ -1383,7 +1387,7 @@
} }
], ],
"source": [ "source": [
"np.random.normal(0, 1, 5) " "np.random.normal(0, 1, 5)\n"
] ]
}, },
{ {
@ -1403,7 +1407,7 @@
} }
], ],
"source": [ "source": [
"np.random.uniform(0, 2, 5)" "np.random.uniform(0, 2, 5)\n"
] ]
}, },
{ {
@ -1453,7 +1457,7 @@
"import numpy as np\n", "import numpy as np\n",
"\n", "\n",
"x = np.array([[1, 2, 3]]).T\n", "x = np.array([[1, 2, 3]]).T\n",
"x.shape" "x.shape\n"
] ]
}, },
{ {
@ -1474,7 +1478,7 @@
], ],
"source": [ "source": [
"xt = x.T\n", "xt = x.T\n",
"xt.shape" "xt.shape\n"
] ]
}, },
{ {
@ -1511,8 +1515,8 @@
} }
], ],
"source": [ "source": [
"x = np.array([[3,4,5,6]]).T\n", "x = np.array([[3, 4, 5, 6]]).T\n",
"x" "x\n"
] ]
}, },
{ {
@ -1543,8 +1547,8 @@
} }
], ],
"source": [ "source": [
"x = np.array([[3,4,5,6]])\n", "x = np.array([[3, 4, 5, 6]])\n",
"x" "x\n"
] ]
}, },
{ {
@ -1570,8 +1574,8 @@
} }
], ],
"source": [ "source": [
"x = np.array([1,2,3,4,5,6,7,8,9]).reshape(3,3)\n", "x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(3, 3)\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -1590,8 +1594,8 @@
} }
], ],
"source": [ "source": [
"y = np.array([4,6,3,8,7,1,3,0,3]).reshape(3,3)\n", "y = np.array([4, 6, 3, 8, 7, 1, 3, 0, 3]).reshape(3, 3)\n",
"print(y)" "print(y)\n"
] ]
}, },
{ {
@ -1601,7 +1605,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"X = np.matrix(x)\n", "X = np.matrix(x)\n",
"Y = np.matrix(y)" "Y = np.matrix(y)\n"
] ]
}, },
{ {
@ -1620,7 +1624,7 @@
} }
], ],
"source": [ "source": [
"print(x * y) # Tablice np.array mnożone są element po elemencie" "print(x * y) # Tablice np.array mnożone są element po elemencie\n"
] ]
}, },
{ {
@ -1641,7 +1645,7 @@
} }
], ],
"source": [ "source": [
"print(X * Y) # Macierze np.matrix mnożone są macierzowo" "print(X * Y) # Macierze np.matrix mnożone są macierzowo\n"
] ]
}, },
{ {
@ -1660,7 +1664,7 @@
} }
], ],
"source": [ "source": [
"print(np.matmul(x, y))" "print(np.matmul(x, y))\n"
] ]
}, },
{ {
@ -1687,8 +1691,8 @@
} }
], ],
"source": [ "source": [
"a = np.array([[3,-9],[2,5]])\n", "a = np.array([[3, -9], [2, 5]])\n",
"np.linalg.det(a)" "np.linalg.det(a)\n"
] ]
}, },
{ {
@ -1716,8 +1720,8 @@
} }
], ],
"source": [ "source": [
"A = np.array([[-4,-2],[5,5]])\n", "A = np.array([[-4, -2], [5, 5]])\n",
"A" "A\n"
] ]
}, },
{ {
@ -1739,7 +1743,7 @@
], ],
"source": [ "source": [
"invA = np.linalg.inv(A)\n", "invA = np.linalg.inv(A)\n",
"invA" "invA\n"
] ]
}, },
{ {
@ -1760,7 +1764,7 @@
} }
], ],
"source": [ "source": [
"np.round(np.dot(A, invA))" "np.round(np.dot(A, invA))\n"
] ]
}, },
{ {
@ -1797,7 +1801,7 @@
], ],
"source": [ "source": [
"a = np.diag((1, 2, 3))\n", "a = np.diag((1, 2, 3))\n",
"a" "a\n"
] ]
}, },
{ {
@ -1819,7 +1823,7 @@
"source": [ "source": [
"w, v = np.linalg.eig(a)\n", "w, v = np.linalg.eig(a)\n",
"print(w) # wartości własne\n", "print(w) # wartości własne\n",
"print(v) # wektory własne" "print(v) # wektory własne\n"
] ]
}, },
{ {
@ -1883,8 +1887,8 @@
"source": [ "source": [
"import torch\n", "import torch\n",
"\n", "\n",
"x = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])\n", "x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -1919,7 +1923,7 @@
"# Wymiary (rozmiar) tensora\n", "# Wymiary (rozmiar) tensora\n",
"\n", "\n",
"print(x.shape)\n", "print(x.shape)\n",
"print(x.size()) # Można użyć `size()` zamiast `shape`" "print(x.size()) # Można użyć `size()` zamiast `shape`\n"
] ]
}, },
{ {
@ -1966,8 +1970,8 @@
} }
], ],
"source": [ "source": [
"x = torch.zeros([3,4])\n", "x = torch.zeros([3, 4])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -1986,8 +1990,8 @@
} }
], ],
"source": [ "source": [
"x = torch.ones([3,4])\n", "x = torch.ones([3, 4])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -2006,8 +2010,8 @@
} }
], ],
"source": [ "source": [
"x = torch.rand([3,4])\n", "x = torch.rand([3, 4])\n",
"print(x)" "print(x)\n"
] ]
}, },
{ {
@ -2046,7 +2050,9 @@
"for i, row in enumerate(x):\n", "for i, row in enumerate(x):\n",
" print(f\"\\nWiersz {i}:\")\n", " print(f\"\\nWiersz {i}:\")\n",
" for element in row:\n", " for element in row:\n",
" print(element.item()) # `item()` zamienia jednoelementowy (bezwymiarowy) tensor na liczbę" " print(\n",
" element.item()\n",
" ) # `item()` zamienia jednoelementowy (bezwymiarowy) tensor na liczbę\n"
] ]
}, },
{ {
@ -2081,7 +2087,7 @@
"print(B)\n", "print(B)\n",
"\n", "\n",
"C = torch.rand([4, 2])\n", "C = torch.rand([4, 2])\n",
"print(C)" "print(C)\n"
] ]
}, },
{ {
@ -2114,7 +2120,7 @@
"print(A + B)\n", "print(A + B)\n",
"print(A - B)\n", "print(A - B)\n",
"print(A * B)\n", "print(A * B)\n",
"print(A / B)" "print(A / B)\n"
] ]
}, },
{ {
@ -2135,7 +2141,7 @@
"source": [ "source": [
"# Mnożenie macierzowe\n", "# Mnożenie macierzowe\n",
"\n", "\n",
"print(torch.matmul(A, C))" "print(torch.matmul(A, C))\n"
] ]
}, },
{ {
@ -2169,7 +2175,7 @@
"print(A)\n", "print(A)\n",
"\n", "\n",
"A_numpy = A.numpy()\n", "A_numpy = A.numpy()\n",
"print(A_numpy)" "print(A_numpy)\n"
] ]
}, },
{ {
@ -2197,7 +2203,7 @@
"print(X)\n", "print(X)\n",
"\n", "\n",
"X_pytorch = torch.from_numpy(X)\n", "X_pytorch = torch.from_numpy(X)\n",
"print(X_pytorch)" "print(X_pytorch)\n"
] ]
}, },
{ {

File diff suppressed because one or more lines are too long