{ "cells": [ { "cell_type": "markdown", "id": "7584fbf6", "metadata": {}, "source": [ "# File input and output with arrays\n", "\n", "NumPy is able to store data in some text or binary formats on disk and load it from there. However, in this section I only discuss NumPy's own binary format, as mostly pandas or other tools are used to load text or table data (see [Read, persist and provide data](../../data-processing/index.rst).\n", "\n", "`np.save` and `np.load` are the two most important functions for efficiently saving and loading array data to disk. Arrays are saved by default in an uncompressed raw binary format with the file extension `.npy`:" ] }, { "cell_type": "code", "execution_count": 1, "id": "d6c92106", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:00:29.001682Z", "iopub.status.busy": "2026-05-17T15:00:29.001384Z", "iopub.status.idle": "2026-05-17T15:00:29.046528Z", "shell.execute_reply": "2026-05-17T15:00:29.046233Z", "shell.execute_reply.started": "2026-05-17T15:00:29.001662Z" } }, "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": "f9b70227", "metadata": {}, "source": [ "If the file path does not already end in `.npy`, the extension is appended. The array on the hard disk can then be loaded with `np.load`:" ] }, { "cell_type": "code", "execution_count": 2, "id": "ed098f7c", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:00:29.047077Z", "iopub.status.busy": "2026-05-17T15:00:29.046981Z", "iopub.status.idle": "2026-05-17T15:00:29.050509Z", "shell.execute_reply": "2026-05-17T15:00:29.050283Z", "shell.execute_reply.started": "2026-05-17T15:00:29.047069Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.69327601, 0.82653332, 0.40657863],\n", " [0.41500074, 0.08436697, 0.75347185],\n", " [0.26657504, 0.65470011, 0.40720279],\n", " [0.32698017, 0.11859362, 0.21459654],\n", " [0.86305016, 0.81381353, 0.12060564],\n", " [0.91046405, 0.58527599, 0.92125107],\n", " [0.85952529, 0.82071675, 0.77440136]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.load(\"my_data.npy\")" ] }, { "cell_type": "markdown", "id": "467f7c50", "metadata": {}, "source": [ "You can save multiple arrays in an uncompressed archive by using `np.savez` and passing the arrays as keyword arguments:" ] }, { "cell_type": "code", "execution_count": 3, "id": "3df6c190", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:00:29.050943Z", "iopub.status.busy": "2026-05-17T15:00:29.050842Z", "iopub.status.idle": "2026-05-17T15:00:29.053301Z", "shell.execute_reply": "2026-05-17T15:00:29.053082Z", "shell.execute_reply.started": "2026-05-17T15:00:29.050930Z" } }, "outputs": [], "source": [ "np.savez(\"data_archive.npz\", a=data, b=np.square(data))" ] }, { "cell_type": "code", "execution_count": 4, "id": "14ea0da5", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:00:29.053662Z", "iopub.status.busy": "2026-05-17T15:00:29.053593Z", "iopub.status.idle": "2026-05-17T15:00:29.056955Z", "shell.execute_reply": "2026-05-17T15:00:29.056680Z", "shell.execute_reply.started": "2026-05-17T15:00:29.053656Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.48063163, 0.68315733, 0.16530618],\n", " [0.17222561, 0.00711779, 0.56771982],\n", " [0.07106225, 0.42863223, 0.16581411],\n", " [0.10691603, 0.01406445, 0.04605167],\n", " [0.74485557, 0.66229247, 0.01454572],\n", " [0.82894478, 0.34254799, 0.84870353],\n", " [0.73878372, 0.67357598, 0.59969746]])" ] }, "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 }