We use np.save() and np.load() to save and load numpy arrays quickly in a compact binary format. This helps keep data safe and easy to reuse.
np.save() and np.load() for binary in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
np.save(filename, array) np.load(filename)
filename should be a string ending with .npy or without extension (it adds .npy automatically).
np.save() saves one array; for multiple arrays, use np.savez().
my_array.npy.import numpy as np arr = np.array([1, 2, 3]) np.save('my_array.npy', arr)
loaded_arr = np.load('my_array.npy') print(loaded_arr)
.npy is added automatically.np.save('data', arr) # saves as 'data.npy'
This program creates a 2D numpy array, saves it to a binary file named saved_array.npy, then loads it back and prints it. This shows how to keep data between program runs.
import numpy as np # Create a 2D array array_to_save = np.array([[10, 20, 30], [40, 50, 60]]) # Save the array to a binary file np.save('saved_array.npy', array_to_save) # Load the array back from the file loaded_array = np.load('saved_array.npy') # Print the loaded array print(loaded_array)
The saved file is in a binary format, so it is not human-readable but loads very fast.
Use np.savez() if you want to save multiple arrays in one file.
Make sure to use the same numpy version when saving and loading to avoid compatibility issues.
np.save() saves one numpy array to a binary file.
np.load() loads the saved array back into memory.
This method is fast and preserves data types exactly.
Practice
np.save() function do in NumPy?Solution
Step 1: Understand the purpose of np.save()
Thenp.save()function is designed to save a NumPy array to a file in binary format, preserving its data type and shape.Step 2: Differentiate from np.load()
np.load()is used to load arrays from files, not save them. Other options do not relate to saving files.Final Answer:
Saves a NumPy array to a binary file on disk -> Option AQuick Check:
np.save() saves array [OK]
- Confusing np.save() with np.load()
- Thinking np.save() converts array to list
- Assuming np.save() prints array
arr to a file named data.npy?Solution
Step 1: Recall np.save() parameter order
The first argument is the filename (string), the second is the array to save.Step 2: Check other options for correctness
np.save(arr, 'data.npy') reverses parameters, np.load('data.npy', arr) uses np.load() which loads, not saves, np.savefile('data.npy', arr) uses a non-existent function.Final Answer:
np.save('data.npy', arr) -> Option DQuick Check:
Filename first, array second in np.save() [OK]
- Swapping filename and array arguments
- Using np.load() instead of np.save() to save
- Using wrong function name like np.savefile()
import numpy as np
arr = np.array([1, 2, 3])
np.save('file.npy', arr)
loaded_arr = np.load('file.npy')
print(loaded_arr)Solution
Step 1: Save and load the array
The array[1, 2, 3]is saved to 'file.npy' and then loaded back exactly as it was.Step 2: Understand print output of loaded array
Printing the loaded array shows the original array as[1 2 3]without quotes or extra brackets.Final Answer:
[1 2 3] -> Option AQuick Check:
np.load(np.save()) returns original array [OK]
- Expecting string elements instead of integers
- Thinking np.load() returns nested arrays
- Assuming file not found error without saving first
import numpy as np
arr = np.array([4, 5, 6])
np.save('mydata.npy')
loaded = np.load('mydata.npy')
print(loaded)Solution
Step 1: Check np.save() usage
Thenp.save()function requires two arguments: filename and array. Here, the array argument is missing.Step 2: Verify other options
np.load() should be called before np.save() is incorrect because loading happens after saving. The filename should have .txt extension is wrong because .npy is the correct extension. np.save() cannot save integer arrays is false; np.save() can save integer arrays.Final Answer:
np.save() is missing the array argument to save -> Option CQuick Check:
np.save() needs filename and array [OK]
- Forgetting to pass the array to np.save()
- Thinking .txt is needed instead of .npy
- Confusing order of np.save() and np.load()
np.save() as arr1.npy and arr2.npy. How can you load both arrays and combine them into a single 2D array where each original array is a row?Solution
Step 1: Load arrays separately
Since arrays are saved in separate files, load each usingnp.load()individually.Step 2: Combine arrays as rows
Usenp.vstack([arr1, arr2])to stack arrays vertically, making each array a row in the new 2D array.Step 3: Check other options
Use np.load('arr1.npy', 'arr2.npy') directly is invalid syntax, Save both arrays in one file using np.save() and then load is incorrect because np.save() saves one array per file, Load arrays and use np.concatenate(arr1, arr2, axis=1) concatenates along columns which may not work if shapes differ.Final Answer:
Load each with np.load() and use np.vstack([arr1, arr2]) -> Option BQuick Check:
Load separately, stack with vstack [OK]
- Trying to load multiple files in one np.load() call
- Using np.concatenate with wrong axis
- Assuming np.save() can save multiple arrays in one file
