🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.save()

AI & DATA SCIENCE // np-save

np.save() writes a single array to disk in NumPy's own binary .npy format, preserving its exact shape, dtype, and values.

Syntax

np.save(file, arr)

Deep Dive Course

The .npy format stores an array's raw data alongside a small header describing its shape and dtype, so loading it back with np.load() reconstructs the exact same array, byte for byte, with none of the precision loss or parsing ambiguity that can come from a text-based format like CSV. It's specifically designed for fast, lossless round-tripping of NumPy arrays between Python sessions, not for interoperability with other tools or human readability.

1Understanding np.save()

The .npy format stores an array's raw data alongside a small header describing its shape and dtype, so loading it back with np.load() reconstructs the exact same array, byte for byte, with none of the precision loss or parsing ambiguity that can come from a text-based format like CSV. It's specifically designed for fast, lossless round-tripping of NumPy arrays between Python sessions, not for interoperability with other tools or human readability.

💡

Use .npy, via save()/load(), for saving intermediate NumPy results within a Python workflow — it's faster to read/write and perfectly lossless, but reach for a text format like CSV specifically when the data needs to be read by other tools or inspected by a human.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
np.save("my_array.npy", arr)
loaded = np.load("my_array.npy")
print(loaded)
localhost:3000

2Practical Example

Here is a real-world application of np.save() showing how it is used in production NumPy code.

editor.html
import numpy as np

original = np.array([1.5, 2.7, 3.14159265358979], dtype=np.float64)
np.save("precise.npy", original)
loaded = np.load("precise.npy")
print(np.array_equal(original, loaded))
localhost:3000

3Best Practices

Follow these guidelines when working with np.save():

1. Use np.save()/np.load() for fast, exact round-tripping of arrays within a Python-only workflow

2. Use a text format like CSV, via savetxt()/loadtxt(), instead when the data needs to be human-readable or opened by non-NumPy tools

3. Let NumPy add the .npy extension automatically rather than fighting it, since save() appends it if the filename doesn't already end with .npy

⚠️

Tip: Use .npy, via save()/load(), for saving intermediate NumPy results within a Python workflow — it's faster to read/write and perfectly lossless, but reach for a text format like CSV specifically when the data needs to be read by other tools or inspected by a human.

editor.html
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
np.save("my_array.npy", arr)
loaded = np.load("my_array.npy")
print(loaded)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
np.save("my_array.npy", arr)
loaded = np.load("my_array.npy")
print(loaded)
Example 02Advanced Example
import numpy as np

original = np.array([1.5, 2.7, 3.14159265358979], dtype=np.float64)
np.save("precise.npy", original)
loaded = np.load("precise.npy")
print(np.array_equal(original, loaded))

Best Practices

  • Use np.save()/np.load() for fast, exact round-tripping of arrays within a Python-only workflow
  • Use a text format like CSV, via savetxt()/loadtxt(), instead when the data needs to be human-readable or opened by non-NumPy tools
  • Let NumPy add the .npy extension automatically rather than fighting it, since save() appends it if the filename doesn't already end with .npy

Interview Question

Why is saving an array with np.save() considered lossless, while saving it as a text file could lose precision?

Hint: Think about how a floating-point number is represented in binary versus as decimal text.

np.save() writes the array's raw binary bytes directly to disk, exactly as they're represented in memory, so loading them back reconstructs the identical bit pattern with zero conversion involved. Writing a float to a text file instead requires converting its binary value into a decimal string representation, and unless you're careful to request enough decimal digits, that conversion can silently round off some of the value's actual binary precision, so reading the text back and reparsing it may not reproduce the exact original bits.

Exercises

MediumPractice using np.save() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
np.save("my_array.npy", arr)
loaded = np.load("my_array.npy")
print(loaded)

Frequently Asked Questions

Why is saving an array with np.save() considered lossless, while saving it as a text file could lose precision?

np.save() writes the array's raw binary bytes directly to disk, exactly as they're represented in memory, so loading them back reconstructs the identical bit pattern with zero conversion involved. Writing a float to a text file instead requires converting its binary value into a decimal string representation, and unless you're careful to request enough decimal digits, that conversion can silently round off some of the value's actual binary precision, so reading the text back and reparsing it may not reproduce the exact original bits.

Related Functions

np-loadnp-savezndarray-dtype