diff --git a/03-numpy-und-matplotlib/02-matrix-sol.ipynb b/03-numpy-und-matplotlib/02-matrix-sol.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..4099b8dd7fd9fd8aa2dc55861effd4fae2fcab0d --- /dev/null +++ b/03-numpy-und-matplotlib/02-matrix-sol.ipynb @@ -0,0 +1,452 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Matrix als 2D-Array\n", + "\n", + "Wir erzeugen in dieser Übung eine Matrix, einen Vektor und führen eine\n", + "Matrix-Vektor-Multiplikation durch.\n", + "\n", + "## a) Matrix erzeugen\n", + "\n", + "Erstellen Sie ein 2D-NumPy-Array `a` mit `arange` und `reshape` wie\n", + "dargestellt:\n", + "\n", + "$\\begin{bmatrix} 1 & 2 & 3\\\\ 4 & 5 & 6 \\end{bmatrix}$\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0005-c6178e60b944258ad3f127adea03eb4ee0da7c74b1a39b54c9400760909" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0006-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lösung zu a)\n", + "\n", + "Wir erstellen einen Vektor von 1, …, 7 (ohne 7) und führen ein `reshape`\n", + "auf $2 \\times 3$ durch um das geforderte Array zu erhalten." + ], + "id": "0008-3ecf6e6b5ab63e42f08e072a90d315657776722faefa171a4c5a4bf408b" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "a = np.arange(1, 7).reshape((2, 3))" + ], + "id": "0009-e4b6c21541fce42f2f2254a5e852efd31e50df101e190790c5cd7b090ec" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu a)\n", + "\n", + "Wir testen, ob die Elemente wie erwartet sind:" + ], + "id": "0011-1cc6e5f1e34374c41c7c0866c7bc97c9953348cde0fc8ef578593078396" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([[1, 2, 3],\n", + " [4, 5, 6]])" + ] + } + ], + "source": [ + "a" + ], + "id": "0012-112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## b) Matrix-Vektor-Multiplikation\n", + "\n", + "Erstellen Sie ein 1D-NumPy-Array `v` mit `arange` wie dargestellt:\n", + "\n", + "$\\begin{bmatrix} 10\\\\ 8\\\\ 6 \\end{bmatrix}$\n", + "\n", + "Führen Sie die Matrix-Vektor-Multiplikation $a \\, v$ durch und speichern\n", + "das Ergebnis in `w`!\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0017-0328546f1dffddc990c09fb5d601b0a937e5de407867597480a75115e3f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0018-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lösung zu b)\n", + "\n", + "Wir erstellen den Vektor von 10, …, 5 mit negativer Schrittweite -2." + ], + "id": "0020-babf4709a23502cf9119e5bb5cce3c40af6d20decb15606ae4514709347" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "v = np.arange(10, 5, -2)" + ], + "id": "0021-4e8d763959efa25db7e48a520463917283a0207d03032bb1fdc4fc21dcc" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nun führen wir die Matrix-Vektor-Multiplikation durch." + ], + "id": "0022-38706663a0a925df41ae4d5761e58c8cc60171cb95935c0e2f0995441f2" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "w = a @ v" + ], + "id": "0023-d45b03c5a5db93eaafa612535b471de9d8e9ba8a32f5b2761a5aa2f51d6" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu b)\n", + "\n", + "Wir testen, ob die Elemente von `v` wie erwartet sind:" + ], + "id": "0025-992f177dd16a0d1b483ed05e06783dc1801f9c0eacc242e35500af5a238" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([10, 8, 6])" + ] + } + ], + "source": [ + "v" + ], + "id": "0026-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und das Ergebnis der Matrix-Vektor-Multiplikation:" + ], + "id": "0027-1ace5586569a4ff843fe20890c8f950f459c0c0377b3c0578ec86be031d" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([ 44, 116])" + ] + } + ], + "source": [ + "w" + ], + "id": "0028-1e49c013f00c62cf59f2163542a9d8df02464efeb615d31051b0fddc326" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## c) Sortierung\n", + "\n", + "Sortieren Sie den Vektor `v` mit `np.sort` und speichern das Ergebnis in\n", + "`v_sorted`! Schauen Sie sich im Test an, ob es aufsteigend oder\n", + "absteigend sortiert ist! Können Sie die Sortierreihenfolge über Slicing\n", + "umkehren? Versuchen Sie es!\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0031-95db6bdf5836af7e18d40db1d82afda3c54771e55900ff5af4368f80a6a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0032-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nun verwenden Sie np.argsort um die Indizes der sortierten Elemente zu\n", + "erhalten und speichern diese in `v_sorted_indices`! Schauen Sie sich im\n", + "Test unten an, was passiert, wenn Sie `v_sorted_indices` auf `v`\n", + "anwenden!\n", + "\n", + "*Bonus: Schaffen Sie es, die Spalten von `a` nach der Sortierung von `v`\n", + "zu sortieren?*\n", + "\n", + "### Lösung zu c)\n", + "\n", + "Zunächst sortieren wir den Vektor `v` aufsteigend und absteigend. Wir\n", + "können die Sortierreihenfolge umkehren, indem wir mit negativer\n", + "Schrittweite slicen." + ], + "id": "0036-eaceadf3fc5eb1dc3b7e431fc79499ba646e7d6c2ad4506f0bb5dfee218" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "v_sorted = np.sort(v) # ascending (aufsteigend)\n", + "v_sorted_reversed = np.sort(v)[::-1] # descending (absteigend)" + ], + "id": "0037-4a6d98a7ec96685ce5f477cc14db5ba7f2da27c9b82e2695e8a3d523b29" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Die Absteigende Sortierung ist natürlich langweilig, weil es die\n", + "ursprüngliche Reihenfolge ist. Nun führen wir die\n", + "Matrix-Vektor-Multiplikation durch." + ], + "id": "0038-27837dafe02bffae4f451b45bf240087053d24ec642144cc73ea2a37fe0" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([10, 8, 6])" + ] + } + ], + "source": [ + "v_sorted_reversed" + ], + "id": "0039-9ff970c568dd84250d1699058e82096ed5be1a0f584a46884def166f8ec" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nun verwenden wir `argsort` um die Indizes der sortierten Elemente zu\n", + "erhalten und speichern diese in `v_sorted_indices`. Man sieht im Test,\n", + "dass die Anwendung der Sortierindizes auf `v` das gleiche Ergebnis wie\n", + "`v_sorted` liefert. Für die Bonusaufgabe wenden wir die Sortierindizes\n", + "auf die Spalten von `a` an. Allgemein lassen sich so zwei Arrays\n", + "gleichartig sortieren, z. B. $y$-Werte nach den entsprechenden\n", + "$x$-Werten." + ], + "id": "0040-f30aad78b36f2c5f31a3c61a51c55c9e43b54b9aa9b4399b0750a26e220" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "v_sorted_indices = np.argsort(v)\n", + "a_sorted = a[:, v_sorted_indices]" + ], + "id": "0041-2443864e0527e20381f777a9b3c01dd0380b4046bcd7b457ed89fedbf1f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([[3, 2, 1],\n", + " [6, 5, 4]])" + ] + } + ], + "source": [ + "a_sorted" + ], + "id": "0042-00c0156accd3cd62646f7445303d5958f50202e08b93250d21117e0e0bf" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu c)\n", + "\n", + "Der Vektor `v` ist:" + ], + "id": "0044-23fffe701d76dc191fa3759ad4ad76eb59b720f4c3ef33d11e45e75011f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([10, 8, 6])" + ] + } + ], + "source": [ + "v" + ], + "id": "0045-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sortiert:" + ], + "id": "0046-fa52a44b66f51668e263ae189ef6422d0aa84c1d63c15b993716154ed71" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([ 6, 8, 10])" + ] + } + ], + "source": [ + "v_sorted" + ], + "id": "0047-fe5508108e9636d2ad10f6c6a611a6fa460ca33e04dda3400c76d4bbe12" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sortierindizes:" + ], + "id": "0048-11a98d7fa4c96482460ec065f0faa4197563223281eb451dc5034ccfced" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([2, 1, 0])" + ] + } + ], + "source": [ + "v_sorted_indices" + ], + "id": "0049-fa89420e96785c287fad6b270873c081d43db4779e9a2503ae62bea9003" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und die Anwendung der Sortierindizes auf `v`:" + ], + "id": "0050-c1108c165d8111c761de5a684ff69c2058d4d52b6c89ba1c589070f56a7" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([ 6, 8, 10])" + ] + } + ], + "source": [ + "v[v_sorted_indices]" + ], + "id": "0051-f0e4f4e230e723cda122e72eb4cc32c10ac8c30139419ccd2df6a8f01c6" + } + ], + "nbformat": 4, + "nbformat_minor": 5, + "metadata": {} +} diff --git a/03-numpy-und-matplotlib/02-matrix.ipynb b/03-numpy-und-matplotlib/02-matrix.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..131fe2b517d750f15f3c502ede9fbadd8f65fba1 --- /dev/null +++ b/03-numpy-und-matplotlib/02-matrix.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Matrix als 2D-Array\n", + "\n", + "Wir erzeugen in dieser Übung eine Matrix, einen Vektor und führen eine\n", + "Matrix-Vektor-Multiplikation durch.\n", + "\n", + "## a) Matrix erzeugen\n", + "\n", + "Erstellen Sie ein 2D-NumPy-Array `a` mit `arange` und `reshape` wie\n", + "dargestellt:\n", + "\n", + "$\\begin{bmatrix} 1 & 2 & 3\\\\ 4 & 5 & 6 \\end{bmatrix}$\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0005-c6178e60b944258ad3f127adea03eb4ee0da7c74b1a39b54c9400760909" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0006-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu a)\n", + "\n", + "Wir testen, ob die Elemente wie erwartet sind:" + ], + "id": "0008-1cc6e5f1e34374c41c7c0866c7bc97c9953348cde0fc8ef578593078396" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([[1, 2, 3],\n", + " [4, 5, 6]])" + ] + } + ], + "source": [ + "a" + ], + "id": "0009-112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## b) Matrix-Vektor-Multiplikation\n", + "\n", + "Erstellen Sie ein 1D-NumPy-Array `v` mit `arange` wie dargestellt:\n", + "\n", + "$\\begin{bmatrix} 10\\\\ 8\\\\ 6 \\end{bmatrix}$\n", + "\n", + "Führen Sie die Matrix-Vektor-Multiplikation $a \\, v$ durch und speichern\n", + "das Ergebnis in `w`!\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0014-0328546f1dffddc990c09fb5d601b0a937e5de407867597480a75115e3f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0015-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu b)\n", + "\n", + "Wir testen, ob die Elemente von `v` wie erwartet sind:" + ], + "id": "0017-992f177dd16a0d1b483ed05e06783dc1801f9c0eacc242e35500af5a238" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([10, 8, 6])" + ] + } + ], + "source": [ + "v" + ], + "id": "0018-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und das Ergebnis der Matrix-Vektor-Multiplikation:" + ], + "id": "0019-1ace5586569a4ff843fe20890c8f950f459c0c0377b3c0578ec86be031d" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([ 44, 116])" + ] + } + ], + "source": [ + "w" + ], + "id": "0020-1e49c013f00c62cf59f2163542a9d8df02464efeb615d31051b0fddc326" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## c) Sortierung\n", + "\n", + "Sortieren Sie den Vektor `v` mit `np.sort` und speichern das Ergebnis in\n", + "`v_sorted`! Schauen Sie sich im Test an, ob es aufsteigend oder\n", + "absteigend sortiert ist! Können Sie die Sortierreihenfolge über Slicing\n", + "umkehren? Versuchen Sie es!\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0023-95db6bdf5836af7e18d40db1d82afda3c54771e55900ff5af4368f80a6a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0024-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nun verwenden Sie np.argsort um die Indizes der sortierten Elemente zu\n", + "erhalten und speichern diese in `v_sorted_indices`! Schauen Sie sich im\n", + "Test unten an, was passiert, wenn Sie `v_sorted_indices` auf `v`\n", + "anwenden!\n", + "\n", + "*Bonus: Schaffen Sie es, die Spalten von `a` nach der Sortierung von `v`\n", + "zu sortieren?*\n", + "\n", + "### Tests zu c)\n", + "\n", + "Der Vektor `v` ist:" + ], + "id": "0028-0df12fdea3eb22d455be855d3463643a0adb3e858fe1e64242c52d75b38" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([10, 8, 6])" + ] + } + ], + "source": [ + "v" + ], + "id": "0029-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sortiert:" + ], + "id": "0030-fa52a44b66f51668e263ae189ef6422d0aa84c1d63c15b993716154ed71" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([ 6, 8, 10])" + ] + } + ], + "source": [ + "v_sorted" + ], + "id": "0031-fe5508108e9636d2ad10f6c6a611a6fa460ca33e04dda3400c76d4bbe12" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sortierindizes:" + ], + "id": "0032-11a98d7fa4c96482460ec065f0faa4197563223281eb451dc5034ccfced" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([2, 1, 0])" + ] + } + ], + "source": [ + "v_sorted_indices" + ], + "id": "0033-fa89420e96785c287fad6b270873c081d43db4779e9a2503ae62bea9003" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und die Anwendung der Sortierindizes auf `v`:" + ], + "id": "0034-c1108c165d8111c761de5a684ff69c2058d4d52b6c89ba1c589070f56a7" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([ 6, 8, 10])" + ] + } + ], + "source": [ + "v[v_sorted_indices]" + ], + "id": "0035-f0e4f4e230e723cda122e72eb4cc32c10ac8c30139419ccd2df6a8f01c6" + } + ], + "nbformat": 4, + "nbformat_minor": 5, + "metadata": {} +} diff --git a/03-numpy-und-matplotlib/02-ziehung-sol.ipynb b/03-numpy-und-matplotlib/03-ziehung-sol.ipynb similarity index 96% rename from 03-numpy-und-matplotlib/02-ziehung-sol.ipynb rename to 03-numpy-und-matplotlib/03-ziehung-sol.ipynb index 869f1c423f27041550d16318a0923486b9b7b06e..b89a7463a91262ca1f0c683317804b831f074130 100644 --- a/03-numpy-und-matplotlib/02-ziehung-sol.ipynb +++ b/03-numpy-und-matplotlib/03-ziehung-sol.ipynb @@ -26,9 +26,9 @@ "## Lösung\n", "\n", "Wir erstellen einen Vektor von 0, …, 20 und führen ein `reshape` auf\n", - "$7 imes 3$ durch um das geforderte Array zu erhalten." + "$7 \\times 3$ durch um das geforderte Array zu erhalten." ], - "id": "0005-c49301c38b440d5f680bae1298919020ffdd23be9734d4417a0f951950c" + "id": "0005-6c4225b83583e52ce194438956dca32831559082e0d85e600bd6eedf084" }, { "cell_type": "code", diff --git a/03-numpy-und-matplotlib/02-ziehung.ipynb b/03-numpy-und-matplotlib/03-ziehung.ipynb similarity index 100% rename from 03-numpy-und-matplotlib/02-ziehung.ipynb rename to 03-numpy-und-matplotlib/03-ziehung.ipynb diff --git a/03-numpy-und-matplotlib/04-kuddelmuddel-sol.ipynb b/03-numpy-und-matplotlib/04-kuddelmuddel-sol.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..8e214458e44e7f470b17edce618a7e789c546cd4 --- /dev/null +++ b/03-numpy-und-matplotlib/04-kuddelmuddel-sol.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Kuddelmuddel\n", + "\n", + "Führen Sie zunächst den Startcode aus und schauen sich den Plot im\n", + "Abschnitt [Test](#tests) an!\n", + "\n", + "Hier ist die Reihenfolge durcheinander geraten. Sortieren Sie $x$ und\n", + "zwar so, dass Sie $y$ entsprechend sortieren können, damit die\n", + "$(x, y)$-Paare noch zusammen passen. Plotten Sie anschließend die Punkte\n", + "mit einem Linienplot." + ], + "id": "0002-bc8683364aac1c6f5ed7b278d3583b1b347acfeb6f1bc5b8c2917727d2f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "x = np.array([ 0., 1.79, 1.58, 0.11, 0.84, 0.53, 1.16, 0.32, 1.89, 1.68, 1.37, 0.21, 0.95, 2., 0.42, 1.26, 0.74, 1.05, 1.47, 0.63])\n", + "y = np.array([ 0., 0.46, 1., 0.5, -0.88, 0.49, -0.47, 1., -0.05, 0.84, 0.53, 0.87, -1., -0.54, 0.86, 0.03, -0.52, -0.85, 0.88, -0.02,])" + ], + "id": "0003-fcca641da37b0e2c663882c5be52b12dcfa29763fdbe639041f2f8705db" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lösung\n", + "\n", + "Damit $y$ noch zu den richtigen $x$ zugeordnet werden kann, sortieren\n", + "wir $x$ nicht direkt, sondern bilden erst den Sortierindex und ordnen\n", + "dann $x$ und $y$ gleichermaßen, indem wir sie mit dem Sortierindex\n", + "indizieren." + ], + "id": "0005-d8d78488067bdbcd104303976ff8c0e94d4a14e6b94cf1aabe757aa1daf" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "sort_ind = np.argsort(x)\n", + "x = x[sort_ind]\n", + "y = y[sort_ind]" + ], + "id": "0006-9951e655ea7e2d7f082cbaaf415a6abc2b9ed7c989b519cdce62b4df1ba" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tests\n", + "\n", + "Hier die Abfrage, ob $x$ sortiert ist:" + ], + "id": "0008-72436094b4e7ff41609abded3de75f7cb16c0f07561e2dcf90a1554132e" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "False" + ] + } + ], + "source": [ + "np.all(np.diff(x) > 0) # differenz zum nächsten Wert jeweils positiv?" + ], + "id": "0009-fdb44a5fb823638e8b19890b97b50cc9b208afe518f1f4ffcc95510ac27" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und der Plot:" + ], + "id": "0010-35ab204c652219970b5613a958ba252c89954e983eb890539b9f924553f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "plt.plot(x, y)" + ], + "id": "0011-3eb6515085572ba2b78abc2ce9f3ebfb4d04c5661dcbd5a964ff73f2fe9" + } + ], + "nbformat": 4, + "nbformat_minor": 5, + "metadata": {} +} diff --git a/03-numpy-und-matplotlib/04-kuddelmuddel.ipynb b/03-numpy-und-matplotlib/04-kuddelmuddel.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..df26604fc4583f2fe29cae5ae85c69f00291b0a5 --- /dev/null +++ b/03-numpy-und-matplotlib/04-kuddelmuddel.ipynb @@ -0,0 +1,87 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Kuddelmuddel\n", + "\n", + "Führen Sie zunächst den Startcode aus und schauen sich den Plot im\n", + "Abschnitt [Test](#tests) an!\n", + "\n", + "Hier ist die Reihenfolge durcheinander geraten. Sortieren Sie $x$ und\n", + "zwar so, dass Sie $y$ entsprechend sortieren können, damit die\n", + "$(x, y)$-Paare noch zusammen passen. Plotten Sie anschließend die Punkte\n", + "mit einem Linienplot." + ], + "id": "0002-bc8683364aac1c6f5ed7b278d3583b1b347acfeb6f1bc5b8c2917727d2f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "x = np.array([ 0., 1.79, 1.58, 0.11, 0.84, 0.53, 1.16, 0.32, 1.89, 1.68, 1.37, 0.21, 0.95, 2., 0.42, 1.26, 0.74, 1.05, 1.47, 0.63])\n", + "y = np.array([ 0., 0.46, 1., 0.5, -0.88, 0.49, -0.47, 1., -0.05, 0.84, 0.53, 0.87, -1., -0.54, 0.86, 0.03, -0.52, -0.85, 0.88, -0.02,])" + ], + "id": "0003-fcca641da37b0e2c663882c5be52b12dcfa29763fdbe639041f2f8705db" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tests\n", + "\n", + "Hier die Abfrage, ob $x$ sortiert ist:" + ], + "id": "0005-72436094b4e7ff41609abded3de75f7cb16c0f07561e2dcf90a1554132e" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "False" + ] + } + ], + "source": [ + "np.all(np.diff(x) > 0) # differenz zum nächsten Wert jeweils positiv?" + ], + "id": "0006-fdb44a5fb823638e8b19890b97b50cc9b208afe518f1f4ffcc95510ac27" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Und der Plot:" + ], + "id": "0007-35ab204c652219970b5613a958ba252c89954e983eb890539b9f924553f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "plt.plot(x, y)" + ], + "id": "0008-3eb6515085572ba2b78abc2ce9f3ebfb4d04c5661dcbd5a964ff73f2fe9" + } + ], + "nbformat": 4, + "nbformat_minor": 5, + "metadata": {} +} diff --git a/03-numpy-und-matplotlib/03-accidents-sol.ipynb b/03-numpy-und-matplotlib/05-accidents-sol.ipynb similarity index 100% rename from 03-numpy-und-matplotlib/03-accidents-sol.ipynb rename to 03-numpy-und-matplotlib/05-accidents-sol.ipynb diff --git a/03-numpy-und-matplotlib/03-accidents.ipynb b/03-numpy-und-matplotlib/05-accidents.ipynb similarity index 100% rename from 03-numpy-und-matplotlib/03-accidents.ipynb rename to 03-numpy-und-matplotlib/05-accidents.ipynb