File I/O: Saving & Loading Native Arrays (.npy, .npz, Text)
Efficient File I/O: .npy, .npz, & CSV/Text Files
Scientific workflows require saving intermediate computations, persisting trained model parameters (such as neural network weights), and loading experimental measurement files.
While CSV or text formats are human-readable, they are notoriously slow to parse and consume immense disk space. NumPy provides optimized binary storage formats (.npy and .npz) that read and write data at raw disk bus bandwidth.
1. Comparing Storage Formats
| Feature | Plain Text / CSV | Single Binary (.npy) | Compressed Archive (.npz) |
|---|---|---|---|
| Speed | Slow (Text $\leftrightarrow$ float conversion) | Lightning fast (raw memory dump) | Fast with high compression |
| File Size | Bloated text characters | Compact binary | Minimal (ZIP compressed) |
| Metadata | None (dtypes/shapes lost) | Preserves shape & dtype | Preserves multiple shapes/dtypes |
| Multi-Array | No (single table only) | Single array only | Dictionary of arrays |
2. Single Array Persistence: .npy
.npy is a simple binary format designed specifically for NumPy ndarrays. It stores the exact shape, dtype, and byte order in a tiny header, followed immediately by the raw binary memory buffer:
3. Multiple Arrays and Compression: .npz
When saving multiple related arrays—such as training data $X_{train}$, labels $y_{train}$, and test sets $X_{test}, y_{test}$—use np.savez() or np.savez_compressed():
4. Memory-Mapped Arrays for Huge Datasets (mmap_mode)
What if you need to inspect a 50 GB dataset on a laptop with only 16 GB of RAM? Standard loading would trigger an Out-of-Memory (OOM) crash.
NumPy supports Memory Mapping (mmap_mode), which maps the array on disk directly into the virtual memory address space. NumPy reads slices from disk on-demand as they are accessed, without loading the full file into RAM:
5. Text / CSV Files: savetxt and genfromtxt
When interoperability with spreadsheets or legacy software requires plain text CSV:
Multiple Choice Questions
1. Which file extension represents NumPy's single binary array storage format?
A. .npz B. .npy C. .dat D. .h5 Answer: B Explanation: .npy is NumPy's standard binary format for persisting a single ndarray along with its shape and dtype metadata.
2. How are multiple arrays saved into a single compressed archive file in NumPy?
A. np.save_all() B. np.savez_compressed() C. np.zip_arrays() D. np.archive() Answer: B Explanation: np.savez_compressed() bundles multiple named arrays into a single compressed .npz file (internally structured as a ZIP archive).
3. What is the key advantage of memory-mapped arrays using np.load(..., mmap_mode='r')?
A. It compresses arrays on disk using encryption B. It allows reading and manipulating datasets larger than available system RAM by loading slices on-demand C. It converts numbers to integers automatically D. It increases GPU clock speed Answer: B Explanation: Memory mapping links the file on disk to virtual address space, reading chunks into memory only as referenced and evicting them when done, enabling out-of-core processing.
4. When loading an .npz file with bundle = np.load('archive.npz'), how do you inspect the names of the saved arrays?
A. bundle.keys B. bundle.files C. bundle.names() D. bundle.columns Answer: B Explanation: An open .npz object provides a .files attribute containing a list of strings representing the internal array keys.
5. Why is .npy substantially faster than CSV for saving and loading numerical data?
A. .npy files are encrypted B. .npy dumps raw contiguous binary bytes directly to disk without costly string conversions or text parsing C. CSV only supports integer values D. NumPy converts CSVs into XML first Answer: B Explanation: Text/CSV requires parsing ASCII characters into IEEE floating-point numbers on every read. .npy performs direct binary memory-to-disk copies at hardware bus speeds.
Capstone: Image Manipulation & Monte Carlo Simulation
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.