Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
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.
| 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 |
.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:
.npzWhen 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():
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:
savetxt and genfromtxtWhen interoperability with spreadsheets or legacy software requires plain text CSV:
np.load(..., mmap_mode='r')?.npz file with bundle = np.load('archive.npz'), how do you inspect the names of the saved arrays?.npy substantially faster than CSV for saving and loading numerical data?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.