{ "cells": [ { "cell_type": "markdown", "id": "8112fccd", "metadata": {}, "source": [ "# `ndarray` – ein N-dimensionales Array-Objekt\n", "\n", "`ndarray` erlaubt mathematische Operationen auf ganzen Datenblöcken und verwendet dabei eine ähnliche Syntax wie bei ähnlichen Operationen zwischen [skalaren](https://de.wikipedia.org/wiki/Skalar_(Mathematik)) Elementen. In NumPy gibt viele verschiedene Typen zur Beschreibung von Skalaren, die größtenteils auf Typen aus der Sprache C basieren und denjenigen, die mit Python kompatibel sind.\n", "\n", "
\n", "\n", "**Siehe auch:**\n", "\n", "* [Array Scalars](https://numpy.org/devdocs/reference/arrays.scalars.html)\n", "
\n", "
\n", "\n", "**Bemerkung:**\n", "\n", "Wann immer in diesem Tutorial von _Array_, _NumPy-Array_ oder `ndarray` geredet wird, bezieht sich dies in den meisten Fällen auf das `ndarray`-Objekt.\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "id": "0ac7f289", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.554358Z", "iopub.status.busy": "2026-05-21T16:59:56.554183Z", "iopub.status.idle": "2026-05-21T16:59:56.591644Z", "shell.execute_reply": "2026-05-21T16:59:56.591329Z", "shell.execute_reply.started": "2026-05-21T16:59:56.554339Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "c7f9f92e", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.592513Z", "iopub.status.busy": "2026-05-21T16:59:56.592185Z", "iopub.status.idle": "2026-05-21T16:59:56.594543Z", "shell.execute_reply": "2026-05-21T16:59:56.594203Z", "shell.execute_reply.started": "2026-05-21T16:59:56.592504Z" } }, "outputs": [], "source": [ "py_list = [2020, 2021, 20222]\n", "array_1d = np.array(py_list)" ] }, { "cell_type": "code", "execution_count": 3, "id": "ee790252", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.595163Z", "iopub.status.busy": "2026-05-21T16:59:56.595072Z", "iopub.status.idle": "2026-05-21T16:59:56.598133Z", "shell.execute_reply": "2026-05-21T16:59:56.597772Z", "shell.execute_reply.started": "2026-05-21T16:59:56.595155Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ 2020, 2021, 20222])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_1d" ] }, { "cell_type": "markdown", "id": "5d4cb94f", "metadata": {}, "source": [ "Verschachtelte Sequenzen, wie eine Liste von Listen gleicher Länge, können in ein mehrdimensionales Array umgewandelt werden:" ] }, { "cell_type": "code", "execution_count": 4, "id": "96d413e8", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.598670Z", "iopub.status.busy": "2026-05-21T16:59:56.598598Z", "iopub.status.idle": "2026-05-21T16:59:56.600299Z", "shell.execute_reply": "2026-05-21T16:59:56.600011Z", "shell.execute_reply.started": "2026-05-21T16:59:56.598663Z" } }, "outputs": [], "source": [ "list_of_lists = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]\n", "array_2d = np.array(list_of_lists)" ] }, { "cell_type": "code", "execution_count": 5, "id": "2762ff48", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.600670Z", "iopub.status.busy": "2026-05-21T16:59:56.600596Z", "iopub.status.idle": "2026-05-21T16:59:56.602821Z", "shell.execute_reply": "2026-05-21T16:59:56.602609Z", "shell.execute_reply.started": "2026-05-21T16:59:56.600663Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4],\n", " [ 5, 6, 7, 8],\n", " [ 9, 10, 11, 12]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_2d" ] }, { "cell_type": "markdown", "id": "e09b6362", "metadata": {}, "source": [ "Da `list_of_lists` eine Liste mit drei Listen war, hat das NumPy-Array `array_2d` zwei Dimensionen, deren Form aus den Daten abgeleitet wird. Mit den Attributen [ndim](https://numpy.org/devdocs/reference/generated/numpy.ndarray.ndim.html) und [shape](https://numpy.org/devdocs/reference/generated/numpy.ndarray.shape.html) können wir uns die Anzahl der Dimensionen und den Umriss von `array_2d` ausgeben lassen:" ] }, { "cell_type": "code", "execution_count": 6, "id": "db6930bc", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.604687Z", "iopub.status.busy": "2026-05-21T16:59:56.604524Z", "iopub.status.idle": "2026-05-21T16:59:56.606700Z", "shell.execute_reply": "2026-05-21T16:59:56.606471Z", "shell.execute_reply.started": "2026-05-21T16:59:56.604676Z" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_2d.ndim" ] }, { "cell_type": "code", "execution_count": 7, "id": "a6fd1f1e", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.607138Z", "iopub.status.busy": "2026-05-21T16:59:56.607046Z", "iopub.status.idle": "2026-05-21T16:59:56.609227Z", "shell.execute_reply": "2026-05-21T16:59:56.608966Z", "shell.execute_reply.started": "2026-05-21T16:59:56.607128Z" } }, "outputs": [ { "data": { "text/plain": [ "(3, 4)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_2d.shape" ] }, { "cell_type": "markdown", "id": "b74ad4c2", "metadata": {}, "source": [ "Um euch einen Eindruck von der Syntax zu vermitteln, erstelle ich zunächst ein Array aus Zufallszahlen mit fünf Spalten und sieben `Slices` (engl.: Scheiben):" ] }, { "cell_type": "code", "execution_count": 8, "id": "6b72ae81", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.609581Z", "iopub.status.busy": "2026-05-21T16:59:56.609497Z", "iopub.status.idle": "2026-05-21T16:59:56.618844Z", "shell.execute_reply": "2026-05-21T16:59:56.618550Z", "shell.execute_reply.started": "2026-05-21T16:59:56.609573Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.39120311, 0.45209947, 0.92648623],\n", " [0.15148821, 0.67249163, 0.84526775],\n", " [0.95089997, 0.15602742, 0.66636488],\n", " [0.21932124, 0.46402051, 0.61522595],\n", " [0.28110203, 0.57970153, 0.21648055],\n", " [0.93219768, 0.45560614, 0.4510681 ],\n", " [0.3103304 , 0.18617668, 0.59796735]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "data" ] }, { "cell_type": "markdown", "id": "e298f51a", "metadata": {}, "source": [ "`ndarray` ist ein generischer mehrdimensionaler Container. Jedes Array hat eine Form, ein Tupel, das die Größe der einzelnen Dimensionen angibt. Mit `shape` kann ich mir die Anzahl der Zeilen und Spalten eines Arrays ausgeben lassen:" ] }, { "cell_type": "markdown", "id": "99a38670", "metadata": {}, "source": [ "Zusätzlich zu `np.array` gibt es eine Reihe weiterer Funktionen zur Erstellung neuer Arrays. [zeros](https://numpy.org/doc/stable/reference/generated/numpy.zeros.html) und [ones](https://numpy.org/doc/stable/reference/generated/numpy.ones.html) erzeugen beispielsweise Arrays aus Nullen bzw. Einsen mit einer bestimmten Länge oder Form. [empty](https://numpy.org/doc/stable/reference/generated/numpy.empty.html) erzeugt ein Array, ohne dessen Werte auf einen bestimmten Wert zu initialisieren. Um ein höherdimensionales Array mit diesen Methoden zu erstellen, übergebt ein Tupel für die Form:" ] }, { "cell_type": "code", "execution_count": 9, "id": "b0bd0d2a", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.619522Z", "iopub.status.busy": "2026-05-21T16:59:56.619417Z", "iopub.status.idle": "2026-05-21T16:59:56.621927Z", "shell.execute_reply": "2026-05-21T16:59:56.621588Z", "shell.execute_reply.started": "2026-05-21T16:59:56.619513Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0.])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(4)" ] }, { "cell_type": "code", "execution_count": 10, "id": "d13f4d63", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.622457Z", "iopub.status.busy": "2026-05-21T16:59:56.622333Z", "iopub.status.idle": "2026-05-21T16:59:56.624884Z", "shell.execute_reply": "2026-05-21T16:59:56.624587Z", "shell.execute_reply.started": "2026-05-21T16:59:56.622448Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((3, 4))" ] }, { "cell_type": "code", "execution_count": 11, "id": "cbe8ee39", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.625353Z", "iopub.status.busy": "2026-05-21T16:59:56.625281Z", "iopub.status.idle": "2026-05-21T16:59:56.627529Z", "shell.execute_reply": "2026-05-21T16:59:56.627246Z", "shell.execute_reply.started": "2026-05-21T16:59:56.625346Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]],\n", "\n", " [[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.empty((2, 3, 4))" ] }, { "cell_type": "markdown", "id": "b88d3c5e", "metadata": {}, "source": [ "
\n", "\n", "**Bemerkung:**\n", "\n", "Ihr dürft nich sicher annehmen, dass die die Funktion `np.empty` ein Array mit lauter Nullen zurückgibt, da sie uninitialisierten Speicher zurückgibt und kann daher auch _garbage_-Werte enthalten kann.\n", "
" ] }, { "cell_type": "markdown", "id": "b2dfa4ce", "metadata": {}, "source": [ "[arange](https://numpy.org/doc/stable/reference/generated/numpy.arange.html) ist eine array-bewertete Version der Built-in Python [range](https://docs.python.org/3/library/functions.html#func-range)-Funktion:" ] }, { "cell_type": "code", "execution_count": 12, "id": "3aba2692", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:59:56.627912Z", "iopub.status.busy": "2026-05-21T16:59:56.627824Z", "iopub.status.idle": "2026-05-21T16:59:56.630222Z", "shell.execute_reply": "2026-05-21T16:59:56.629941Z", "shell.execute_reply.started": "2026-05-21T16:59:56.627904Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(4)" ] }, { "cell_type": "markdown", "id": "a38ff6ae", "metadata": {}, "source": [ "Weitere NumPy-Standardfunktionen zur Erstellung von Arrays sind:\n", "\n", "Funktion | Beschreibung\n", ":------- | :-----------\n", "`array` | konvertiert Eingabedaten (Liste, Tupel, Array oder andere Sequenztypen) in ein `ndarray`, indem entweder ein `dtype` abgeleitet oder explizit ein `dtype` angegeben wird; kopiert standardmäßig die Eingabedaten in das Array\n", "`asarray` | konvertiert die Eingabe in ein `ndarray`, kopiert aber nicht, wenn die Eingabe bereits ein `ndarray` ist\n", "`arange` | wie Python built-in `range`, gibt aber ein `ndarray` statt einer Liste zurück\n", "`ones`, `ones_like` | `ones` erzeugt ein Array mit 1en in der gegebenen Form und dem gegebenen `dtype`; `ones_like` nimmt ein anderes Array und erzeugt ein `ones`-Array in der gleichen Form und dem gleichen `dtype`\n", "`zeros`, `zeros_like` | wie `ones` und `ones_like`, erzeugt aber stattdessen Arrays mit 0en\n", "`empty`, `empty_like` | erzeugt neue Arrays durch Zuweisung neuen Speichers, füllt sie aber nicht mit Werten wie `ones` und `zeros`\n", "`full`, `full_like` | erzeugt ein Array der angegebenen `shape` und des angegebenen `dtype`, wobei alle Werte auf den angegebenen _Füllwert_ gesetzt werden; `full_like` nimmt ein anderes Array und erzeugt ein gefülltes Array mit denselben `shape` und `dtype`\n", "`eye`, `identity` | erzeugt eine quadratische N × N-Identitätsmatrix (1en auf der Diagonale und 0en anderswo)" ] } ], "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 }