{ "cells": [ { "cell_type": "markdown", "id": "c03a7d37", "metadata": {}, "source": [ "# Mathematische und statistische Methoden\n", "\n", "Eine Reihe von mathematischen Funktionen, die Statistiken über ein ganzes Array oder über die Daten entlang einer Achse berechnen, sind als Methoden der Array-Klasse zugänglich. So könnt ihr Aggregationen verwenden wie Summe, Mittelwert und Standardabweichung, indem ihr entweder die Array-Instanzmethode aufruft oder die NumPy-Funktion der obersten Ebene verwendet." ] }, { "cell_type": "markdown", "id": "4013204e", "metadata": {}, "source": [ "Im folgenden generiere ich einige normalverteilte Zufallsdaten und berechne einige aggregierte Statistiken:" ] }, { "cell_type": "code", "execution_count": 1, "id": "b413c7b0", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.653175Z", "iopub.status.busy": "2026-05-21T16:57:07.652785Z", "iopub.status.idle": "2026-05-21T16:57:07.698255Z", "shell.execute_reply": "2026-05-21T16:57:07.697969Z", "shell.execute_reply.started": "2026-05-21T16:57:07.653153Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.79440596, 0.10972775, 0.35435383],\n", " [0.74358108, 0.43438519, 0.83743312],\n", " [0.94298041, 0.39124331, 0.08939286],\n", " [0.18968082, 0.60070664, 0.5113487 ],\n", " [0.76300762, 0.88587544, 0.6838369 ],\n", " [0.714206 , 0.17504049, 0.58097537],\n", " [0.22871752, 0.95094866, 0.99379828]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "456e9376", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.698752Z", "iopub.status.busy": "2026-05-21T16:57:07.698616Z", "iopub.status.idle": "2026-05-21T16:57:07.701331Z", "shell.execute_reply": "2026-05-21T16:57:07.701091Z", "shell.execute_reply.started": "2026-05-21T16:57:07.698742Z" } }, "outputs": [ { "data": { "text/plain": [ "np.float64(0.5702688544567261)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.mean()" ] }, { "cell_type": "code", "execution_count": 3, "id": "c8dc9d4e", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.701873Z", "iopub.status.busy": "2026-05-21T16:57:07.701764Z", "iopub.status.idle": "2026-05-21T16:57:07.703966Z", "shell.execute_reply": "2026-05-21T16:57:07.703688Z", "shell.execute_reply.started": "2026-05-21T16:57:07.701862Z" } }, "outputs": [ { "data": { "text/plain": [ "np.float64(0.5702688544567261)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(data)" ] }, { "cell_type": "code", "execution_count": 4, "id": "5092c04d", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.704489Z", "iopub.status.busy": "2026-05-21T16:57:07.704386Z", "iopub.status.idle": "2026-05-21T16:57:07.707083Z", "shell.execute_reply": "2026-05-21T16:57:07.706829Z", "shell.execute_reply.started": "2026-05-21T16:57:07.704481Z" } }, "outputs": [ { "data": { "text/plain": [ "np.float64(11.97564594359125)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum()" ] }, { "cell_type": "markdown", "id": "b67e9edd", "metadata": {}, "source": [ "Funktionen wie `mean` und `sum` benötigen ein optionales Achsenargument, das die Statistik über die angegebene Achse berechnet, was zu einem Array mit einer Dimension weniger führt:" ] }, { "cell_type": "code", "execution_count": 5, "id": "d84656ba", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.707743Z", "iopub.status.busy": "2026-05-21T16:57:07.707633Z", "iopub.status.idle": "2026-05-21T16:57:07.710074Z", "shell.execute_reply": "2026-05-21T16:57:07.709787Z", "shell.execute_reply.started": "2026-05-21T16:57:07.707735Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0.62522563, 0.50684678, 0.57873415])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.mean(axis=0)" ] }, { "cell_type": "code", "execution_count": 6, "id": "b7657887", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.711579Z", "iopub.status.busy": "2026-05-21T16:57:07.711477Z", "iopub.status.idle": "2026-05-21T16:57:07.713919Z", "shell.execute_reply": "2026-05-21T16:57:07.713703Z", "shell.execute_reply.started": "2026-05-21T16:57:07.711572Z" } }, "outputs": [ { "data": { "text/plain": [ "array([4.3765794 , 3.54792749, 4.05113905])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sum(axis=0)" ] }, { "cell_type": "markdown", "id": "ca7e65c0", "metadata": {}, "source": [ "Mit `data.mean(0)`, was dasselbe ist wie `data.mean(axis=0)`, wird der Mittelwert über die Zeilen berechnet, während `data.sum(0)` die Summe über die Zeilen berechnet." ] }, { "cell_type": "markdown", "id": "84530009", "metadata": {}, "source": [ "Andere Methoden wie `cumsum` und `cumprod` aggregieren hingegen nicht, sondern erzeugen ein neues Array mit den Zwischenergebnissen.\n", "\n", "In mehrdimensionalen Arrays geben Akkumulationsfunktionen wie `cumsum` und `cumprodm` ein Array derselben Größe zurück, aber mit den entlang der angegebenen Achse berechneten Teilaggregaten:" ] }, { "cell_type": "code", "execution_count": 7, "id": "d50cfc21", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.714237Z", "iopub.status.busy": "2026-05-21T16:57:07.714182Z", "iopub.status.idle": "2026-05-21T16:57:07.716485Z", "shell.execute_reply": "2026-05-21T16:57:07.716267Z", "shell.execute_reply.started": "2026-05-21T16:57:07.714231Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.79440596, 0.90413371, 1.25848754, 2.00206862, 2.43645381,\n", " 3.27388693, 4.21686734, 4.60811065, 4.69750351, 4.88718433,\n", " 5.48789098, 5.99923968, 6.76224729, 7.64812274, 8.33195963,\n", " 9.04616563, 9.22120611, 9.80218148, 10.030899 , 10.98184767,\n", " 11.97564594])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.cumsum()" ] }, { "cell_type": "code", "execution_count": 8, "id": "a6524b2b", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:57:07.716804Z", "iopub.status.busy": "2026-05-21T16:57:07.716734Z", "iopub.status.idle": "2026-05-21T16:57:07.718850Z", "shell.execute_reply": "2026-05-21T16:57:07.718631Z", "shell.execute_reply.started": "2026-05-21T16:57:07.716796Z" } }, "outputs": [ { "data": { "text/plain": [ "array([7.94405957e-01, 8.71683779e-02, 3.08884486e-02, 2.29680659e-02,\n", " 9.97698767e-03, 8.35505991e-03, 7.87865783e-03, 3.08247220e-03,\n", " 2.75551012e-04, 5.22667412e-05, 3.13969787e-05, 1.60548043e-05,\n", " 1.22499380e-05, 1.08519192e-05, 7.42094275e-06, 5.30008181e-06,\n", " 9.27728893e-07, 5.38987633e-07, 1.23275916e-07, 1.17229068e-07,\n", " 1.16502046e-07])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.cumprod()" ] }, { "cell_type": "markdown", "id": "b73111f2", "metadata": {}, "source": [ "Grundlegende statistische Methoden für Arrays:\n", "\n", "Methode | Beschreibung\n", ":------ | :-----------\n", "`sum` | Summe aller Elemente im Array oder entlang einer Achse\n", "`mean` | Arithmetisches Mittel; bei Arrays mit der Länge Null wird`NaN` zurückgegeben \n", "`std`, `var` | Standardabweichung bzw. Varianz\n", "`min`, `max` | Minimum und Maximum\n", "`argmin`, `argmax` | Indizes der minimalen bzw. maximalen Elemente\n", "`cumsum` | Kumulative Summe der Elemente, beginnend mit `0`\n", "`cumprod` | Kumulatives Produkt der Elemente, beginnend mit `1`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }