{ "cells": [ { "cell_type": "markdown", "id": "ab82ed77", "metadata": {}, "source": [ "# Dateieingabe und -ausgabe mit Arrays\n", "\n", "NumPy ist in der Lage, Daten in einigen Text- oder Binärformaten auf der Festplatte zu speichern und von dort zu laden. In diesem Abschnitt gehe ich jedoch nur auf das NumPy-eigene Binärformat ein, da meist pandas oder andere Werkzeuge zum Laden von Text- oder Tabellendaten verwendet werden (siehe [Daten lesen, speichern und bereitstellen](../../data-processing/index.rst)).\n", "\n", "`np.save` und `np.load` sind die beiden wichtigsten Funktionen zum effizienten Speichern und Laden von Array-Daten auf der Festplatte. Arrays werden standardmäßig in einem unkomprimierten Rohbinärformat mit der Dateierweiterung `.npy` gespeichert:" ] }, { "cell_type": "code", "execution_count": 1, "id": "87d74bba", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:20:21.625120Z", "iopub.status.busy": "2026-05-21T17:20:21.624893Z", "iopub.status.idle": "2026-05-21T17:20:21.670117Z", "shell.execute_reply": "2026-05-21T17:20:21.669808Z", "shell.execute_reply.started": "2026-05-21T17:20:21.625101Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "\n", "np.save(\"my_data\", data)" ] }, { "cell_type": "markdown", "id": "9ce05e9b", "metadata": {}, "source": [ "Wenn der Dateipfad nicht bereits auf `.npy` endet, wird die Erweiterung angehängt. Das Array auf der Festplatte kann dann mit `np.load` geladen werden:" ] }, { "cell_type": "code", "execution_count": 2, "id": "d8e5a54f", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:20:21.670518Z", "iopub.status.busy": "2026-05-21T17:20:21.670420Z", "iopub.status.idle": "2026-05-21T17:20:21.674388Z", "shell.execute_reply": "2026-05-21T17:20:21.674083Z", "shell.execute_reply.started": "2026-05-21T17:20:21.670510Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.32713476, 0.26072411, 0.97066123],\n", " [0.64324948, 0.22916615, 0.45848477],\n", " [0.74193241, 0.25614138, 0.14923645],\n", " [0.85910429, 0.13530806, 0.57281031],\n", " [0.8111816 , 0.55065099, 0.40271298],\n", " [0.97844806, 0.73711443, 0.2190297 ],\n", " [0.33551703, 0.49527855, 0.08432219]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.load(\"my_data.npy\")" ] }, { "cell_type": "markdown", "id": "761e6276", "metadata": {}, "source": [ "Ihr könnt mehrere Arrays in einem unkomprimierten Archiv speichern indem ihr `np.savez` verwendet und die Arrays als Schlüsselwortargumente übergebt:" ] }, { "cell_type": "code", "execution_count": 3, "id": "f5886d8c", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:20:21.675110Z", "iopub.status.busy": "2026-05-21T17:20:21.675018Z", "iopub.status.idle": "2026-05-21T17:20:21.677559Z", "shell.execute_reply": "2026-05-21T17:20:21.677250Z", "shell.execute_reply.started": "2026-05-21T17:20:21.675102Z" } }, "outputs": [], "source": [ "np.savez(\"data_archive.npz\", a=data, b=np.square(data))" ] }, { "cell_type": "code", "execution_count": 4, "id": "f1ff70cd", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:20:21.677998Z", "iopub.status.busy": "2026-05-21T17:20:21.677938Z", "iopub.status.idle": "2026-05-21T17:20:21.681474Z", "shell.execute_reply": "2026-05-21T17:20:21.681215Z", "shell.execute_reply.started": "2026-05-21T17:20:21.677992Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.10701715, 0.06797706, 0.94218322],\n", " [0.41376989, 0.05251713, 0.21020829],\n", " [0.55046371, 0.06560841, 0.02227152],\n", " [0.73806019, 0.01830827, 0.32811166],\n", " [0.65801558, 0.30321652, 0.16217775],\n", " [0.9573606 , 0.54333768, 0.04797401],\n", " [0.11257167, 0.24530084, 0.00711023]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "archive = np.load(\"data_archive.npz\")\n", "\n", "archive[\"b\"]" ] } ], "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 }