From 3bfaf3762547c8dffb369f3ddbd2e77ff341c6d4 Mon Sep 17 00:00:00 2001 From: Christof Kaufmann <christof.kaufmann@hs-bochum.de> Date: Mon, 7 Apr 2025 12:31:35 +0200 Subject: [PATCH] Quick-Fix --- 03-numpy-und-matplotlib/02-matrix.ipynb | 67 +++- 03-numpy-und-matplotlib/04-mean-temp.ipynb | 147 +++++++++ .../solutions/02-matrix-sol.ipynb | 90 +++++- .../solutions/04-mean-temp-sol.ipynb | 294 ++++++++++++++++++ 4 files changed, 590 insertions(+), 8 deletions(-) create mode 100644 03-numpy-und-matplotlib/04-mean-temp.ipynb create mode 100644 03-numpy-und-matplotlib/solutions/04-mean-temp-sol.ipynb diff --git a/03-numpy-und-matplotlib/02-matrix.ipynb b/03-numpy-und-matplotlib/02-matrix.ipynb index 131fe2b..6a61bd7 100644 --- a/03-numpy-und-matplotlib/02-matrix.ipynb +++ b/03-numpy-und-matplotlib/02-matrix.ipynb @@ -14,11 +14,14 @@ "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", + "$\\begin{bmatrix}\n", + "1 & 2 & 3\\\\\n", + "4 & 5 & 6\n", + "\\end{bmatrix}$\n", "\n", "Hier Ihr Code:" ], - "id": "0005-c6178e60b944258ad3f127adea03eb4ee0da7c74b1a39b54c9400760909" + "id": "0005-db440e97cb5c7f546520ed47264b7fce12db0ccfc7c8adb03bca9113aee" }, { "cell_type": "code", @@ -67,14 +70,18 @@ "\n", "Erstellen Sie ein 1D-NumPy-Array `v` mit `arange` wie dargestellt:\n", "\n", - "$\\begin{bmatrix} 10\\\\ 8\\\\ 6 \\end{bmatrix}$\n", + "$\\begin{bmatrix}\n", + "10\\\\\n", + "8\\\\\n", + "6\n", + "\\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" + "id": "0014-298594a5981320e04d208dd7d227a173f8a4fa65f04cad0e233cf939a12" }, { "cell_type": "code", @@ -278,6 +285,58 @@ "v[v_sorted_indices]" ], "id": "0035-f0e4f4e230e723cda122e72eb4cc32c10ac8c30139419ccd2df6a8f01c6" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## d) Zeile hinzufügen\n", + "\n", + "Fügen Sie $v$ als Zeile zu $a$ hinzu und speichern es in `av`!\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0038-60ff5efbf91d48180e27b1e4e4dd3074404e32192dc1356f79296273c72" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0039-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu d)\n", + "\n", + "Wir testen, ob die Elemente von `av` wie erwartet sind:" + ], + "id": "0041-6c3e8a5629f3b4c854cdac323a8871364284522e5a15f6974a77f00269f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([[ 1, 2, 3],\n", + " [ 4, 5, 6],\n", + " [10, 8, 6]])" + ] + } + ], + "source": [ + "av" + ], + "id": "0042-bca625b223971909dd88fc93faeb050dc5b34d91c0871661740dcfb9d18" } ], "nbformat": 4, diff --git a/03-numpy-und-matplotlib/04-mean-temp.ipynb b/03-numpy-und-matplotlib/04-mean-temp.ipynb new file mode 100644 index 0000000..6de7e1f --- /dev/null +++ b/03-numpy-und-matplotlib/04-mean-temp.ipynb @@ -0,0 +1,147 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mittlere Temperaturen\n", + "\n", + "Die Funktion `load_temp` lädt die mittlere Monatstemperaturen von Januar\n", + "2005 bis August 2023 von der Wetterstation 01303 (Essen-Bredeney) als\n", + "NumPy-Array:\n", + "\n", + "| 0: Jahr | 1: Monat | 2: Temperatur in K |\n", + "|--------:|---------:|-------------------:|\n", + "| 2005 | 1 | 277.02 |\n", + "| 2005 | 2 | 274.15 |\n", + "| 2005 | 3 | 279.44 |\n", + "| … | … | … |\n", + "| 2023 | 7 | 291.75 |\n", + "| 2023 | 8 | 291.18 |\n", + "\n", + "Mitteln Sie die Temperaturen für…\n", + "\n", + "1. …die Monate über alle Jahre und speichern es in `month_mean`. So\n", + " erhalten Sie beispielsweise die mittlere Temperatur im Januar\n", + " (unabhängig vom Jahr). Plotten Sie die Ergebnisse.\n", + "2. …die Jahre über alle Monate und speichern es in `year_mean`. So\n", + " erhalten Sie beispielsweise die mittlere Temperatur im Jahr 2005\n", + " (unabhängig vom Monat). Plotten Sie die Ergebnisse.\n", + "\n", + "Hier Ihr Startcode:" + ], + "id": "0005-d2ed2a9b5930bbb53dab5d18ab5a1c4bcaabe44815e106bcb42bb7411c2" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt\n", + "\n", + "def load_temp():\n", + " FILE_PATH = 'dwd_data-2023-08-31.parquet.zst'\n", + " dwd_data = pd.read_parquet(FILE_PATH)\n", + "\n", + " station_ind = dwd_data['station_id'] == '01303'\n", + " param_ind = dwd_data['parameter'] == 'temperature_air_mean_200'\n", + " dwd_data = dwd_data[station_ind & param_ind]\n", + "\n", + " dwd_data['year'] = dwd_data['date'].dt.year\n", + " dwd_data['month'] = dwd_data['date'].dt.month\n", + " return dwd_data[['year', 'month', 'value']].to_numpy()\n", + "\n", + "temp = load_temp()" + ], + "id": "0006-e4d3cff1c8267ff2b520c715c7e3fb808d00dc6a5155c21ece66a06e444" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tests\n", + "\n", + "Wir schauen mal, ob die `shape` passt:" + ], + "id": "0008-539fc374ab04b1689d3d56775fe49569971d6ae370acf0fb866780c7864" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(12,)" + ] + } + ], + "source": [ + "np.shape(month_mean)" + ], + "id": "0009-bcadab89753cff51d9c3ccf5822c5cd62857e16f60b147f35e7b6f32dfa" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der Mittelwert für Januar sollte ca. 276.276 K betragen:" + ], + "id": "0010-df2a515e50faefff59e6b552fa4caa1ab108f7d3c2098a41c24ee3680d6" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "276.276 K" + ] + } + ], + "source": [ + "print(f'{month_mean[0]:.3f} K')" + ], + "id": "0011-91b4879873e05a15752b382e6910b0f9907260a15a24e461078caa00fac" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der Mittelwert für 2005 sollte ca. 283.485 K betragen:" + ], + "id": "0012-1851b721844dfa31100516a6bdf306ffb883139d381e456ac704cab86a3" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "283.485 K" + ] + } + ], + "source": [ + "print(f'{year_mean[0]:.3f} K')" + ], + "id": "0013-0d97c8b776aa3b2fe55e04bb02e3c8f8b31bc133c4aea242af15ff8d9cd" + } + ], + "nbformat": 4, + "nbformat_minor": 5, + "metadata": {} +} diff --git a/03-numpy-und-matplotlib/solutions/02-matrix-sol.ipynb b/03-numpy-und-matplotlib/solutions/02-matrix-sol.ipynb index 4099b8d..4bba2ec 100644 --- a/03-numpy-und-matplotlib/solutions/02-matrix-sol.ipynb +++ b/03-numpy-und-matplotlib/solutions/02-matrix-sol.ipynb @@ -14,11 +14,14 @@ "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", + "$\\begin{bmatrix}\n", + "1 & 2 & 3\\\\\n", + "4 & 5 & 6\n", + "\\end{bmatrix}$\n", "\n", "Hier Ihr Code:" ], - "id": "0005-c6178e60b944258ad3f127adea03eb4ee0da7c74b1a39b54c9400760909" + "id": "0005-db440e97cb5c7f546520ed47264b7fce12db0ccfc7c8adb03bca9113aee" }, { "cell_type": "code", @@ -92,14 +95,18 @@ "\n", "Erstellen Sie ein 1D-NumPy-Array `v` mit `arange` wie dargestellt:\n", "\n", - "$\\begin{bmatrix} 10\\\\ 8\\\\ 6 \\end{bmatrix}$\n", + "$\\begin{bmatrix}\n", + "10\\\\\n", + "8\\\\\n", + "6\n", + "\\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" + "id": "0017-298594a5981320e04d208dd7d227a173f8a4fa65f04cad0e233cf939a12" }, { "cell_type": "code", @@ -444,6 +451,81 @@ "v[v_sorted_indices]" ], "id": "0051-f0e4f4e230e723cda122e72eb4cc32c10ac8c30139419ccd2df6a8f01c6" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## d) Zeile hinzufügen\n", + "\n", + "Fügen Sie $v$ als Zeile zu $a$ hinzu und speichern es in `av`!\n", + "\n", + "Hier Ihr Code:" + ], + "id": "0054-60ff5efbf91d48180e27b1e4e4dd3074404e32192dc1356f79296273c72" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [], + "id": "0055-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lösung zu d)\n", + "\n", + "Wir verwenden `np.vstack` um die Zeile `v` zu `a` hinzuzufügen. Das\n", + "Ergebnis ist ein $3 \\times 3$-Array." + ], + "id": "0057-97452acebf5b4c664fe1476c7982190acc0b2e6dcb4126eab30867457f1" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "av = np.vstack((a, v))" + ], + "id": "0058-7137f073beec5783a38b5737aa69b976bdd4863d0afc94e6cc871586665" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tests zu d)\n", + "\n", + "Wir testen, ob die Elemente von `av` wie erwartet sind:" + ], + "id": "0060-6c3e8a5629f3b4c854cdac323a8871364284522e5a15f6974a77f00269f" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([[ 1, 2, 3],\n", + " [ 4, 5, 6],\n", + " [10, 8, 6]])" + ] + } + ], + "source": [ + "av" + ], + "id": "0061-bca625b223971909dd88fc93faeb050dc5b34d91c0871661740dcfb9d18" } ], "nbformat": 4, diff --git a/03-numpy-und-matplotlib/solutions/04-mean-temp-sol.ipynb b/03-numpy-und-matplotlib/solutions/04-mean-temp-sol.ipynb new file mode 100644 index 0000000..6c8c6a4 --- /dev/null +++ b/03-numpy-und-matplotlib/solutions/04-mean-temp-sol.ipynb @@ -0,0 +1,294 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mittlere Temperaturen\n", + "\n", + "Die Funktion `load_temp` lädt die mittlere Monatstemperaturen von Januar\n", + "2005 bis August 2023 von der Wetterstation 01303 (Essen-Bredeney) als\n", + "NumPy-Array:\n", + "\n", + "| 0: Jahr | 1: Monat | 2: Temperatur in K |\n", + "|--------:|---------:|-------------------:|\n", + "| 2005 | 1 | 277.02 |\n", + "| 2005 | 2 | 274.15 |\n", + "| 2005 | 3 | 279.44 |\n", + "| … | … | … |\n", + "| 2023 | 7 | 291.75 |\n", + "| 2023 | 8 | 291.18 |\n", + "\n", + "Mitteln Sie die Temperaturen für…\n", + "\n", + "1. …die Monate über alle Jahre und speichern es in `month_mean`. So\n", + " erhalten Sie beispielsweise die mittlere Temperatur im Januar\n", + " (unabhängig vom Jahr). Plotten Sie die Ergebnisse.\n", + "2. …die Jahre über alle Monate und speichern es in `year_mean`. So\n", + " erhalten Sie beispielsweise die mittlere Temperatur im Jahr 2005\n", + " (unabhängig vom Monat). Plotten Sie die Ergebnisse.\n", + "\n", + "Hier Ihr Startcode:" + ], + "id": "0005-d2ed2a9b5930bbb53dab5d18ab5a1c4bcaabe44815e106bcb42bb7411c2" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "import matplotlib.pyplot as plt\n", + "\n", + "def load_temp():\n", + " FILE_PATH = 'dwd_data-2023-08-31.parquet.zst'\n", + " dwd_data = pd.read_parquet(FILE_PATH)\n", + "\n", + " station_ind = dwd_data['station_id'] == '01303'\n", + " param_ind = dwd_data['parameter'] == 'temperature_air_mean_200'\n", + " dwd_data = dwd_data[station_ind & param_ind]\n", + "\n", + " dwd_data['year'] = dwd_data['date'].dt.year\n", + " dwd_data['month'] = dwd_data['date'].dt.month\n", + " return dwd_data[['year', 'month', 'value']].to_numpy()\n", + "\n", + "temp = load_temp()" + ], + "id": "0006-e4d3cff1c8267ff2b520c715c7e3fb808d00dc6a5155c21ece66a06e444" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lösung\n", + "\n", + "Um für alle Monate (Spalte 1) die mittlere Temperatur (Spalte 2) zu\n", + "berechnen, erstellen wir zunächst eine Sequenz von 1 bis 12. Dann\n", + "verwenden wir eine List Comprehension um für jeden Monat die mittlere\n", + "Temperatur zu berechnen. Dabei ergibt `temp[:, 1] == month` einen\n", + "booleschen Vektor, der angibt, ob die Zeile zu dem Monat gehört. Mit\n", + "`temp[temp[:, 1] == month, 2]` erhalten wir dann alle Temperaturen für\n", + "den Monat, die wir mit `np.mean(...)` mitteln." + ], + "id": "0008-2cd25c8798f51f8e5a6c713c29b493ac19b894f0ed5571805f0e364e845" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "all_months = range(1, 13)\n", + "month_mean = [np.mean(temp[temp[:, 1] == month, 2]) for month in all_months]" + ], + "id": "0009-a1253165c038b338462cc82eb29e2f7c48a4a9fc2d4445491a01a7c03c4" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Das Ergebnis plotten wir ganz einfach mit:" + ], + "id": "0010-ecadd0c3a7487df23a98b0962f4f82f4be5e1843bc8ae8e7e27ef69f618" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "plt.figure()\n", + "plt.plot(month_mean)" + ], + "id": "0011-3a213d48af32df5375e47a6ecccc9845858537761ddb421498a332a8fb0" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Um für alle Jahre (Spalte 0) die mittlere Temperatur (Spalte 2) zu\n", + "berechnen, verwenden wir eine ähnliche List Comprehension. Wir verwenden\n", + "`np.unique` um die Jahre sortiert zu extrahieren. Das ergibt einfach\n", + "eine Sequenz von 2005 bis 2023. Der Aufruf von `astype(int)` wandelt die\n", + "Werte in Integer um, ist aber nicht wichtig. Vor der Mittelung filtern\n", + "wir 2023 weg, weil es unvollständig ist und der Mittelwert zu hoch wäre,\n", + "da die kalten Temperaturen von September bis Dezember fehlen würden.\n", + "Auch dafür hätte man wieder eine `range(2005, 2023)` verwenden können." + ], + "id": "0012-3ee0a3d69a51adfd7eaf1f4213775cd4d7d7715664ece17e6e89b6ed8fb" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "all_years = np.unique(temp[:, 0]).astype(int)\n", + "all_years = all_years[all_years < 2023]\n", + "year_mean = [np.mean(temp[temp[:, 0] == year, 2]) for year in all_years]" + ], + "id": "0013-ca8699b005a925df38cf6316af64aaf256f49174a83f449e6b447b1cf19" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Das Ergebnis plotten wir ganz einfach mit:" + ], + "id": "0014-ecadd0c3a7487df23a98b0962f4f82f4be5e1843bc8ae8e7e27ef69f618" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "style": "python" + }, + "outputs": [], + "source": [ + "plt.figure()\n", + "plt.plot(all_years, year_mean)" + ], + "id": "0015-a42d856cfe30a9bcf7f2c514ee6b65372103e8818cf5eb0a0e17d6e6d69" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wenn wir nicht wüssten, ob noch weitere Jahre außer 2023 unvollständig\n", + "sind, könnten wir bei `np.unique` auch `return_counts=True` verwenden:" + ], + "id": "0016-ed8aabb9d71daa8b180cb6773528e73ff5928d72d7ea8b8e83f7439fc1a" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "array([12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 8])" + ] + } + ], + "source": [ + "all_years, counts = np.unique(temp[:, 0], return_counts=True)\n", + "counts" + ], + "id": "0017-d45e342d4be912931cb0a80ee62eec2a2377fb20ca59fea9ea11428f6dd" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nun lassen sich die Jahre mit weniger als 12 Monaten einfach\n", + "herausfiltern:" + ], + "id": "0018-59550f7dd84c46441758b975aa0b718731fbdc69f9992e9aff1daa99955" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "all_years = all_years[counts == 12]" + ], + "id": "0019-c5dafbe42aea1dcd7419a1e90abdede4001c4eb471ff52286729745fd4b" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tests\n", + "\n", + "Wir schauen mal, ob die `shape` passt:" + ], + "id": "0021-539fc374ab04b1689d3d56775fe49569971d6ae370acf0fb866780c7864" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(12,)" + ] + } + ], + "source": [ + "np.shape(month_mean)" + ], + "id": "0022-bcadab89753cff51d9c3ccf5822c5cd62857e16f60b147f35e7b6f32dfa" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der Mittelwert für Januar sollte ca. 276.276 K betragen:" + ], + "id": "0023-df2a515e50faefff59e6b552fa4caa1ab108f7d3c2098a41c24ee3680d6" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "276.276 K" + ] + } + ], + "source": [ + "print(f'{month_mean[0]:.3f} K')" + ], + "id": "0024-91b4879873e05a15752b382e6910b0f9907260a15a24e461078caa00fac" + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Der Mittelwert für 2005 sollte ca. 283.485 K betragen:" + ], + "id": "0025-1851b721844dfa31100516a6bdf306ffb883139d381e456ac704cab86a3" + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "283.485 K" + ] + } + ], + "source": [ + "print(f'{year_mean[0]:.3f} K')" + ], + "id": "0026-0d97c8b776aa3b2fe55e04bb02e3c8f8b31bc133c4aea242af15ff8d9cd" + } + ], + "nbformat": 4, + "nbformat_minor": 5, + "metadata": {} +} -- GitLab