{ "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": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "\n", "data = np.random.randn(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": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2.30602873, 0.01517455, -1.17839484],\n", " [ 1.34255075, -0.08358613, -0.71584898],\n", " [-2.18879985, -0.24203697, -0.64047467],\n", " [ 1.22940807, 0.81555884, -0.92011422],\n", " [-1.2429792 , -0.16863888, 1.0725737 ],\n", " [-1.08591637, 1.19503417, -2.37629923],\n", " [-0.56407652, -0.42741029, 0.79441985]])" ] }, "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": {}, "outputs": [], "source": [ "np.savez(\"data_archive.npz\", a=data, b=np.square(data))" ] }, { "cell_type": "code", "execution_count": 4, "id": "14ea0da5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[5.31776850e+00, 2.30267094e-04, 1.38861441e+00],\n", " [1.80244252e+00, 6.98664125e-03, 5.12439756e-01],\n", " [4.79084477e+00, 5.85818951e-02, 4.10207800e-01],\n", " [1.51144420e+00, 6.65136217e-01, 8.46610174e-01],\n", " [1.54499728e+00, 2.84390717e-02, 1.15041434e+00],\n", " [1.17921437e+00, 1.42810668e+00, 5.64679805e+00],\n", " [3.18182316e-01, 1.82679555e-01, 6.31102899e-01]])" ] }, "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 }