{ "cells": [ { "cell_type": "markdown", "id": "28d71ca9", "metadata": {}, "source": [ "## `dtype`" ] }, { "cell_type": "markdown", "id": "2c1dc791", "metadata": {}, "source": [ "Der `ndarray` ist ein Container für homogene Daten, d.h. alle Elemente müssen vom gleichen Typ sein. Jedes Array hat einen `dtype`, ein Objekt, das den Datentyp des Arrays beschreibt:" ] }, { "cell_type": "code", "execution_count": 1, "id": "6b2b2120", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:21:31.426945Z", "iopub.status.busy": "2026-05-21T17:21:31.426761Z", "iopub.status.idle": "2026-05-21T17:21:31.483895Z", "shell.execute_reply": "2026-05-21T17:21:31.483634Z", "shell.execute_reply.started": "2026-05-21T17:21:31.426923Z" } }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "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", "dt = data.dtype\n", "dt" ] }, { "cell_type": "markdown", "id": "e3db94d2", "metadata": {}, "source": [ "NumPy-Datentypen:" ] }, { "cell_type": "markdown", "id": "10e9fbac", "metadata": {}, "source": [ "Typ | Typ-Code | Beschreibung\n", ":---------------- | :------------ | :-----------\n", "`int8`, `uint8` | `i1`, `u1` | Vorzeichenbehaftete und vorzeichenlose 8-Bit (1 Byte) Ganzzahltypen\n", "`int16`, `uint16` | `i2`, `u2` | Vorzeichenbehaftete und vorzeichenlose 16-Bit (2 Byte) Ganzzahltypen\n", "`int32`, `uint32` | `i4`, `u4` | Vorzeichenbehaftete und vorzeichenlose 32-Bit (4 Byte) Ganzzahltypen\n", "`int64`, `uint64` | `i8`, `u8` | Vorzeichenbehaftete und vorzeichenlose 64-Bit (8 Byte) Ganzzahltypen\n", "`float16` | `f2` | Standard-Gleitkomma mit halber Genauigkeit\n", "`float32` | `f4` oder `f` | Standard-Gleitkomma mit einfacher Genauigkeit; kompatibel mit C `float`\n", "`float64` | `f8` oder `d` | Standard-Gleitkomma mit doppelter Genauigkeit; kompatibel mit C `double` und Python `float`-Objekt\n", "`complex64`, `complex128`, `complex256` | `c8`, `c16`, `c32` | Komplexe Zahlen, die durch zwei 32-, 64- bzw. 128-Gleitkommazahlen dargestellt werden\n", "`bool` | `?` | Boolescher Typ, der die Werte `True` und `False` speichert\n", "`object` | `O` | Python-Objekttyp; ein Wert kann ein beliebiges Python-Objekt sein\n", "`string_` | `S` | ASCII-Stringtyp mit fester Länge (1 Byte pro Zeichen); um z.B. einen Stringtyp mit der Länge 7 zu erstellen, verwendet `S7`; längere Eingaben werden ohne Warnung abgeschnitten \n", "`unicode_` | `U` | Unicode-Typ mit fester Länge wobei die Anzahl der Bytes plattformspezifisch ist; verwendet dieselbe Spezifikationssemantik wie `string_`, z.B. `U7`" ] }, { "cell_type": "markdown", "id": "5f9ade8b", "metadata": {}, "source": [ "Anzahl der Elemente mit `itemsize` ermitteln:" ] }, { "cell_type": "code", "execution_count": 2, "id": "f10f4595", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:21:31.484477Z", "iopub.status.busy": "2026-05-21T17:21:31.484351Z", "iopub.status.idle": "2026-05-21T17:21:31.486956Z", "shell.execute_reply": "2026-05-21T17:21:31.486631Z", "shell.execute_reply.started": "2026-05-21T17:21:31.484469Z" } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.itemsize" ] }, { "cell_type": "markdown", "id": "0abca49a", "metadata": {}, "source": [ "Name des Datentypes ermitteln:" ] }, { "cell_type": "code", "execution_count": 3, "id": "7c4dd8aa", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:21:31.487466Z", "iopub.status.busy": "2026-05-21T17:21:31.487389Z", "iopub.status.idle": "2026-05-21T17:21:31.489484Z", "shell.execute_reply": "2026-05-21T17:21:31.489216Z", "shell.execute_reply.started": "2026-05-21T17:21:31.487458Z" } }, "outputs": [ { "data": { "text/plain": [ "'float64'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.name" ] }, { "cell_type": "markdown", "id": "c739fce8", "metadata": {}, "source": [ "Datentyp überprüfen:" ] }, { "cell_type": "code", "execution_count": 4, "id": "23dafd4e", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:21:31.489998Z", "iopub.status.busy": "2026-05-21T17:21:31.489915Z", "iopub.status.idle": "2026-05-21T17:21:31.491779Z", "shell.execute_reply": "2026-05-21T17:21:31.491562Z", "shell.execute_reply.started": "2026-05-21T17:21:31.489991Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.type is np.float64" ] }, { "cell_type": "markdown", "id": "13be7929", "metadata": {}, "source": [ "Datentyp ändern mit `astype`:" ] }, { "cell_type": "code", "execution_count": 5, "id": "d2eff3e7", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:21:31.492190Z", "iopub.status.busy": "2026-05-21T17:21:31.492098Z", "iopub.status.idle": "2026-05-21T17:21:31.494714Z", "shell.execute_reply": "2026-05-21T17:21:31.494365Z", "shell.execute_reply.started": "2026-05-21T17:21:31.492184Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.8959591 , 0.95819676, 0.52365017],\n", " [0.05653278, 0.18683353, 0.76510984],\n", " [0.9283983 , 0.561397 , 0.8671029 ],\n", " [0.86952984, 0.21159811, 0.6124638 ],\n", " [0.03761354, 0.41308585, 0.5807331 ],\n", " [0.9768227 , 0.3987061 , 0.5606387 ],\n", " [0.5677797 , 0.4074486 , 0.9794231 ]], dtype=float32)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_float32 = data.astype(np.float32)\n", "data_float32" ] } ], "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 }