We use np.savez() to save many arrays into one file. This helps keep data organized and easy to load later.
np.savez() for multiple arrays in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
import numpy as np np.savez(filename, array1, array2, ..., arrayN) # Or with names: np.savez(filename, name1=array1, name2=array2, ...)
The filename should end with .npz but it is optional.
If you give names, you can load arrays by those names later.
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) np.savez('data.npz', array1, array2)
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) np.savez('data_named.npz', first=array1, second=array2)
import numpy as np # Edge case: saving no arrays np.savez('empty.npz')
import numpy as np single_array = np.array([10]) np.savez('single.npz', single_array)
This program creates two arrays, saves them into one file with names, then loads and prints them to show they are saved correctly.
import numpy as np # Create arrays ages = np.array([25, 30, 35]) scores = np.array([88, 92, 95]) print('Before saving:') print('Ages:', ages) print('Scores:', scores) # Save arrays with names np.savez('people_data.npz', ages=ages, scores=scores) # Load saved arrays loaded_data = np.load('people_data.npz') print('\nAfter loading:') print('Ages:', loaded_data['ages']) print('Scores:', loaded_data['scores'])
Time complexity is O(n) where n is total elements saved, because data is copied to disk.
Space complexity depends on the size of arrays saved.
Common mistake: forgetting to use names and then not knowing which array is which when loading.
Use np.savez() when you want to save multiple arrays in one file. Use np.save() for a single array.
np.savez() saves multiple arrays into one file for easy storage and sharing.
You can save arrays with or without names; names help when loading.
Loading the file gives access to each array by its name or default names like 'arr_0'.
Practice
np.savez() in NumPy?Solution
Step 1: Understand the function purpose
np.savez()is used to save multiple arrays into one file, making storage and sharing easier.Step 2: Compare options with function use
Loading arrays is done bynp.load(), notnp.savez(). Creating or deleting arrays is unrelated.Final Answer:
To save multiple arrays into a single file for easy storage -> Option AQuick Check:
np.savez() saves arrays [OK]
- Confusing np.savez() with np.load()
- Thinking it creates new arrays
- Assuming it deletes arrays
a and b using np.savez() with names?Solution
Step 1: Recall named saving syntax
To save arrays with names, usenp.savez(filename, name1=array1, name2=array2).Step 2: Check each option
np.savez('data.npz', first=a, second=b) uses named arguments correctly. np.savez('data.npz', a, b) saves unnamed arrays. Options B and D misuse argument positions and names.Final Answer:
np.savez('data.npz', first=a, second=b) -> Option DQuick Check:
Named arrays use name=array [OK]
- Not using names for arrays
- Passing arrays as positional but expecting names
- Mixing argument order
np.savez('file.npz', x, y) where x and y are arrays without names?Solution
Step 1: Understand default naming in np.savez()
If arrays are saved without names, NumPy assigns default keys like 'arr_0', 'arr_1', etc.Step 2: Match output keys
Sincexandywere saved unnamed, keys will be 'arr_0' and 'arr_1'. Named keys like 'x' or 'y' appear only if explicitly named.Final Answer:
['arr_0', 'arr_1'] -> Option AQuick Check:
Unnamed arrays get keys arr_0, arr_1 [OK]
- Assuming variable names become keys automatically
- Expecting numeric string keys
- Using arbitrary names without naming arrays
import numpy as np
x = np.array([1,2])
y = np.array([3,4])
np.savez('data.npz', x, y)
loaded = np.load('data.npz')
print(loaded['x'])Solution
Step 1: Check how arrays were saved
Arraysxandywere saved without names, so keys are 'arr_0' and 'arr_1'.Step 2: Analyze the loading and key access
Trying to accessloaded['x']causes a KeyError because 'x' is not a key in the file.Final Answer:
KeyError because 'x' was not saved with a name -> Option BQuick Check:
Unnamed arrays have keys arr_0, arr_1 [OK]
- Assuming variable names are keys automatically
- Ignoring KeyError on wrong key access
- Confusing save and load syntax
a, b, and c into one file with names 'first', 'second', and 'third'. Later, you want to load and print the sum of all elements from these arrays. Which code correctly does this?Solution
Step 1: Save arrays with correct names
np.savez('arrays.npz', first=a, second=b, third=c) loaded = np.load('arrays.npz') total = loaded['first'].sum() + loaded['second'].sum() + loaded['third'].sum() print(total) saves arrays with names 'first', 'second', 'third' matching the requirement.Step 2: Load and sum elements correctly
np.savez('arrays.npz', first=a, second=b, third=c) loaded = np.load('arrays.npz') total = loaded['first'].sum() + loaded['second'].sum() + loaded['third'].sum() print(total) accesses arrays by correct keys and sums elements using.sum(). Other options either use wrong keys or incorrect summing methods.Final Answer:
np.savez('arrays.npz', first=a, second=b, third=c) loaded = np.load('arrays.npz') total = loaded['first'].sum() + loaded['second'].sum() + loaded['third'].sum() print(total) -> Option CQuick Check:
Save with names, load by names, sum elements [OK]
- Using wrong keys when loading
- Trying to sum arrays directly without .sum()
- Saving arrays without names but accessing by names
