From c14db6430a4c23507ee45e87e5681c3d4bb7caee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Sk=C3=B3rzewski?= Date: Thu, 3 Mar 2022 11:16:35 +0100 Subject: [PATCH] =?UTF-8?q?Aktualizacja=20materia=C5=82=C3=B3w=20o=20Pytho?= =?UTF-8?q?nie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab/01_Python.ipynb | 383 +++++++++++++++++--------------------------- 1 file changed, 150 insertions(+), 233 deletions(-) diff --git a/lab/01_Python.ipynb b/lab/01_Python.ipynb index 4ca5a6d..98bffb7 100644 --- a/lab/01_Python.ipynb +++ b/lab/01_Python.ipynb @@ -22,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -108,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -117,7 +117,7 @@ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, - "execution_count": 56, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } @@ -128,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 29, "metadata": { "scrolled": true }, @@ -148,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -166,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 31, "metadata": {}, "outputs": [ { @@ -187,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -205,7 +205,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 33, "metadata": {}, "outputs": [ { @@ -235,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -262,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 35, "metadata": {}, "outputs": [ { @@ -296,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 36, "metadata": {}, "outputs": [ { @@ -323,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -352,7 +352,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -382,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -428,7 +428,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -451,7 +451,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -473,7 +473,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 42, "metadata": {}, "outputs": [ { @@ -494,7 +494,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -511,7 +511,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -526,9 +526,35 @@ "print(y.shape)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Korzystając z funkcji `shape`, można znaleźć liczbę wierszy i kolumn macierzy:" + ] + }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Liczba kolumn: 2\n", + "Liczba wierszy: 5\n" + ] + } + ], + "source": [ + "print(\"Liczba kolumn: \", y.shape[0])\n", + "print(\"Liczba wierszy: \", y.shape[1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [ { @@ -550,7 +576,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -567,7 +593,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -593,7 +619,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -620,7 +646,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -641,7 +667,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -653,12 +679,21 @@ } ], "source": [ - "print(z.sum(axis=1))" + "print(z.sum(axis=1)) # suma według wierszy" ] }, { "cell_type": "code", - "execution_count": 79, + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(z.sum(axis=0)) # suma według kolumn" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [ { @@ -679,7 +714,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -698,7 +733,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -719,7 +754,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -737,7 +772,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -752,12 +787,12 @@ } ], "source": [ - "x.sum(axis=0)" + "x.sum(axis=0) # suma " ] }, { "cell_type": "code", - "execution_count": 84, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -784,7 +819,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -804,7 +839,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -824,7 +859,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -860,7 +895,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": null, "metadata": { "scrolled": true }, @@ -892,7 +927,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -918,7 +953,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -979,7 +1014,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1012,7 +1047,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1032,7 +1067,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1052,7 +1087,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1071,7 +1106,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1092,7 +1127,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1113,7 +1148,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1133,7 +1168,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1152,7 +1187,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1173,7 +1208,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": null, "metadata": { "scrolled": true }, @@ -1192,7 +1227,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1213,7 +1248,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1234,7 +1269,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1269,7 +1304,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1290,7 +1325,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1308,7 +1343,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1326,7 +1361,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1344,7 +1379,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1362,7 +1397,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1383,7 +1418,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": null, "metadata": { "scrolled": true }, @@ -1428,7 +1463,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1449,7 +1484,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1477,7 +1512,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1496,7 +1531,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1515,7 +1550,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1536,7 +1571,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1564,7 +1599,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1587,7 +1622,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1609,7 +1644,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1637,7 +1672,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1660,7 +1695,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1689,7 +1724,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1710,7 +1745,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1730,7 +1765,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1750,7 +1785,7 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1770,7 +1805,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1805,7 +1840,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1826,7 +1861,7 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1846,7 +1881,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1873,7 +1908,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1896,7 +1931,7 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1916,7 +1951,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1936,7 +1971,7 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1956,7 +1991,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1991,7 +2026,7 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2012,7 +2047,7 @@ }, { "cell_type": "code", - "execution_count": 139, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2032,7 +2067,7 @@ }, { "cell_type": "code", - "execution_count": 141, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2052,7 +2087,7 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2072,7 +2107,7 @@ }, { "cell_type": "code", - "execution_count": 143, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2099,7 +2134,7 @@ }, { "cell_type": "code", - "execution_count": 145, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2119,7 +2154,7 @@ }, { "cell_type": "code", - "execution_count": 146, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2149,7 +2184,7 @@ }, { "cell_type": "code", - "execution_count": 147, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2186,7 +2221,7 @@ }, { "cell_type": "code", - "execution_count": 148, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2206,7 +2241,7 @@ }, { "cell_type": "code", - "execution_count": 149, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2226,7 +2261,7 @@ }, { "cell_type": "code", - "execution_count": 150, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2280,7 +2315,7 @@ }, { "cell_type": "code", - "execution_count": 172, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2300,7 +2335,7 @@ }, { "cell_type": "code", - "execution_count": 173, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2319,7 +2354,7 @@ }, { "cell_type": "code", - "execution_count": 151, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2342,7 +2377,7 @@ }, { "cell_type": "code", - "execution_count": 152, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2377,7 +2412,7 @@ }, { "cell_type": "code", - "execution_count": 153, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2412,7 +2447,7 @@ }, { "cell_type": "code", - "execution_count": 154, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2440,7 +2475,7 @@ }, { "cell_type": "code", - "execution_count": 158, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2460,7 +2495,7 @@ }, { "cell_type": "code", - "execution_count": 159, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2480,7 +2515,7 @@ }, { "cell_type": "code", - "execution_count": 160, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2490,7 +2525,7 @@ }, { "cell_type": "code", - "execution_count": 161, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2509,7 +2544,7 @@ }, { "cell_type": "code", - "execution_count": 162, + "execution_count": null, "metadata": { "scrolled": true }, @@ -2530,7 +2565,7 @@ }, { "cell_type": "code", - "execution_count": 164, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2556,7 +2591,7 @@ }, { "cell_type": "code", - "execution_count": 165, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2584,7 +2619,7 @@ }, { "cell_type": "code", - "execution_count": 166, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2606,7 +2641,7 @@ }, { "cell_type": "code", - "execution_count": 167, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2628,7 +2663,7 @@ }, { "cell_type": "code", - "execution_count": 168, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2663,7 +2698,7 @@ }, { "cell_type": "code", - "execution_count": 169, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2686,7 +2721,7 @@ }, { "cell_type": "code", - "execution_count": 170, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -2705,124 +2740,6 @@ "print(w) # wartości własne\n", "print(v) # wektory własne" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Zadania" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Zadanie 1.1 (1 pkt)\n", - "\n", - "Dla danej listy `input_list` zawierającej liczby utwórz nową listę `output_list`, która będzie zawierała kwadraty liczb dodatnich z `input_list`. Użyj _list comprehension_!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Przykładowe dane\n", - "\n", - "input_list = [34.6, -203.4, 44.9, 68.3, -12.2, 44.6, 12.7]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Zadanie 1.2 (1 pkt)\n", - "\n", - "Za pomocą jednowierszowego polecenia utwórz następującą macierz jako obiekt typu `array`:\n", - "$$A = \\begin{pmatrix}\n", - "1 & 2 & \\cdots & 10 \\\\\n", - "11 & 12 & \\cdots & 20 \\\\\n", - "\\vdots & \\ddots & \\ddots & \\vdots \\\\\n", - "41 & 42 & \\cdots & 50 \n", - "\\end{pmatrix}$$" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Zadanie 1.3 (1 pkt)\n", - "\n", - "Dla macierzy $A$ z zadania 1.2:\n", - " * określ liczbę elementów, kolumn i wierszy,\n", - " * stwórz wektory średnich po wierszach oraz po kolumnach,\n", - " * wypisz jej trzecią kolumnę,\n", - " * wypisz jej czwarty wiersz.\n", - " \n", - "Użyj odpowiednich metod obiektu `array`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Zadanie 1.4 (1 pkt)\n", - "\n", - "Utwórz macierze\n", - "$$ A = \\begin{pmatrix}\n", - "0 & 4 & -2 \\\\\n", - "-4 & -3 & 0\n", - "\\end{pmatrix} $$\n", - "$$ B = \\begin{pmatrix}\n", - "0 & 1 \\\\\n", - "1 & -1 \\\\\n", - "2 & 3\n", - "\\end{pmatrix} $$\n", - "oraz wektor\n", - "$$ x = \\begin{pmatrix}\n", - "2 \\\\\n", - "1 \\\\\n", - "0\n", - "\\end{pmatrix} $$\n", - "\n", - "Oblicz:\n", - " * iloczyn macierzy $A$ z wektorem $x$ \n", - " * iloczyn macierzy $A \\cdot B$\n", - " * wyznacznik $\\det(A \\cdot B)$\n", - " * wynik działania $(A \\cdot B)^\\top - B^\\top \\cdot A^\\top$" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Zadanie 1.5 (1 pkt)\n", - "\n", - "Czym różni się operacja `A**-1` dla obiektów typu `array` i `matrix`? Pokaż na przykładzie." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Zadanie 1.6 (1 pkt)\n", - "\n", - "Dla macierzy $X = \\left[\n", - " \\begin{array}{rrr}\n", - " 1 & 2 & 3\\\\\n", - " 1 & 3 & 6 \\\\\n", - " \\end{array}\n", - " \\right]$ oraz wektora $y = \\left[\n", - " \\begin{array}{r}\n", - " 5 \\\\\n", - " 6 \\\\\n", - " \\end{array}\n", - " \\right]$ oblicz wynikowy wektor: \n", - "$$ \\theta = (X^\\top \\, X)^{-1} \\, X^\\top \\, y \\, . $$\n", - "Wykonaj te same obliczenia raz na obiektach typu `array`, a raz na obiektach typu `matrix`.\n", - "W przypadku obiektów typu `matrix` zastosuj możliwie krótki zapis. " - ] } ], "metadata": {