0
0
NumPydata~10 mins

np.save() and np.load() for binary in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.save() and np.load() for binary
Create numpy array
Call np.save(filename, array)
Array saved as binary file
Call np.load(filename)
Binary file read into numpy array
Use loaded array in program
First, you save a numpy array to a binary file using np.save(). Later, you load it back with np.load() to get the same array.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
np.save('data.npy', arr)
loaded_arr = np.load('data.npy')
print(loaded_arr)
This code saves a numpy array to a file and then loads it back, printing the loaded array.
Execution Table
StepActionInput/ConditionResult/Output
1Create arrayarr = [1, 2, 3]arr is numpy array [1 2 3]
2Save arraynp.save('data.npy', arr)File 'data.npy' created with binary data
3Load arrayloaded_arr = np.load('data.npy')loaded_arr is numpy array [1 2 3]
4Print loaded arrayprint(loaded_arr)Output: [1 2 3]
5EndNo more codeExecution stops
💡 All steps completed, array saved and loaded successfully
Variable Tracker
VariableStartAfter Step 1After Step 3Final
arrundefined[1 2 3][1 2 3][1 2 3]
loaded_arrundefinedundefined[1 2 3][1 2 3]
Key Moments - 3 Insights
Why does np.save() create a file instead of returning data?
np.save() writes the array directly to a binary file on disk, so it does not return data but creates a file you can load later (see step 2 in execution_table).
What type is the object returned by np.load()?
np.load() returns a numpy array loaded from the binary file, exactly like the original array (see step 3 and 4 in execution_table).
Can you use np.load() without saving first?
No, np.load() needs a file created by np.save() or similar. If the file doesn't exist, it will cause an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'loaded_arr' after step 3?
Aundefined
B[1 2 3]
C[3 2 1]
DNone
💡 Hint
Check the 'Result/Output' column for step 3 in execution_table.
At which step is the binary file 'data.npy' created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the action 'Save array' in execution_table.
If you change the array before saving to [4,5,6], what will be printed at step 4?
A[4 5 6]
BError
C[1 2 3]
DEmpty array
💡 Hint
The saved file stores the array at save time, so loading will return the saved array (see variable_tracker).
Concept Snapshot
np.save(filename, array) saves a numpy array to a binary file.
np.load(filename) loads the array back from the file.
The saved file has .npy extension by convention.
Use these to store and retrieve arrays efficiently.
np.save does not return data, it writes to disk.
np.load returns the original numpy array.
Full Transcript
This visual execution shows how np.save() writes a numpy array to a binary file and np.load() reads it back. First, an array is created. Then np.save() writes it to 'data.npy'. Next, np.load() reads the file and returns the array. Finally, printing shows the loaded array matches the original. Variables 'arr' and 'loaded_arr' track the array before and after saving/loading. Key points include understanding that np.save creates a file and np.load returns a numpy array. The quiz checks understanding of variable values and file creation steps.