Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Save Multiple Arrays Using np.savez()
📖 Scenario: Imagine you are working with sensor data collected from different devices. Each device produces a list of measurements. You want to save all these lists into a single file so you can easily load them later for analysis.
🎯 Goal: You will create multiple NumPy arrays representing sensor data, then save them together into one file using np.savez(). Finally, you will print the names of the saved arrays to confirm the save was successful.
📋 What You'll Learn
Create three NumPy arrays with exact values
Create a filename variable to store the output file name
Use np.savez() to save all arrays into one file with specific names
Print the names of the saved arrays from the saved file
💡 Why This Matters
🌍 Real World
Saving multiple arrays in one file is useful when working with data from different sensors or experiments. It keeps data organized and easy to share.
💼 Career
Data scientists often save and load multiple datasets efficiently. Knowing how to use <code>np.savez()</code> helps manage data files in projects.
Progress0 / 4 steps
1
Create three NumPy arrays
Import NumPy as np. Create three arrays called sensor1, sensor2, and sensor3 with these exact values: sensor1 = np.array([10, 20, 30]), sensor2 = np.array([15, 25, 35]), sensor3 = np.array([12, 22, 32]).
NumPy
Hint
Use np.array() to create each array with the exact numbers.
2
Create a filename variable
Create a variable called filename and set it to the string 'sensors_data.npz'.
NumPy
Hint
Just assign the string 'sensors_data.npz' to the variable filename.
3
Save arrays into one file using np.savez()
Use np.savez() to save sensor1, sensor2, and sensor3 into the file named filename. Name the arrays inside the file as 's1', 's2', and 's3' respectively.
NumPy
Hint
Use np.savez(filename, s1=sensor1, s2=sensor2, s3=sensor3) to save all arrays with names.
4
Load and print saved array names
Load the saved file using np.load(filename) as data. Then print the list of array names stored in data.files.
NumPy
Hint
Use data = np.load(filename) and then print(list(data.files)) to see the saved array names.
Practice
(1/5)
1. What is the main purpose of np.savez() in NumPy?
easy
A. To save multiple arrays into a single file for easy storage
B. To load arrays from a file
C. To create a new array from existing ones
D. To delete arrays from memory
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 by np.load(), not np.savez(). Creating or deleting arrays is unrelated.
Final Answer:
To save multiple arrays into a single file for easy storage -> Option A
Quick Check:
np.savez() saves arrays [OK]
Hint: Remember: savez saves multiple arrays in one file [OK]
Common Mistakes:
Confusing np.savez() with np.load()
Thinking it creates new arrays
Assuming it deletes arrays
2. Which of the following is the correct syntax to save two arrays a and b using np.savez() with names?
easy
A. np.savez('data.npz', a, b)
B. np.savez(a='data.npz', b='data.npz')
C. np.savez('data.npz', a=b)
D. np.savez('data.npz', first=a, second=b)
Solution
Step 1: Recall named saving syntax
To save arrays with names, use np.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 D
Quick Check:
Named arrays use name=array [OK]
Hint: Use name=value pairs to save arrays with names [OK]
Common Mistakes:
Not using names for arrays
Passing arrays as positional but expecting names
Mixing argument order
3. What will be the output keys when loading a file saved by np.savez('file.npz', x, y) where x and y are arrays without names?
medium
A. ['arr_0', 'arr_1']
B. ['x', 'y']
C. ['0', '1']
D. ['array1', 'array2']
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
Since x and y were 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 A
Quick Check:
Unnamed arrays get keys arr_0, arr_1 [OK]
Hint: Unnamed arrays get keys arr_0, arr_1, ... by default [OK]
Common Mistakes:
Assuming variable names become keys automatically
Expecting numeric string keys
Using arbitrary names without naming arrays
4. Identify the error in this code snippet:
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'])
medium
A. SyntaxError in np.savez() call
B. KeyError because 'x' was not saved with a name
C. FileNotFoundError when loading 'data.npz'
D. No error, prints array x
Solution
Step 1: Check how arrays were saved
Arrays x and y were saved without names, so keys are 'arr_0' and 'arr_1'.
Step 2: Analyze the loading and key access
Trying to access loaded['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 B
Quick Check:
Unnamed arrays have keys arr_0, arr_1 [OK]
Hint: Access keys as arr_0 if arrays saved without names [OK]
Common Mistakes:
Assuming variable names are keys automatically
Ignoring KeyError on wrong key access
Confusing save and load syntax
5. You want to save three arrays 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?
hard
A. np.savez('arrays.npz', first=a, second=b, third=c)
loaded = np.load('arrays.npz')
total = sum(loaded.values())
print(total)
B. np.savez('arrays.npz', a, b, c)
loaded = np.load('arrays.npz')
total = loaded['a'].sum() + loaded['b'].sum() + loaded['c'].sum()
print(total)
C. 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)
D. np.savez('arrays.npz', a=a, b=b, c=c)
loaded = np.load('arrays.npz')
total = loaded['first'] + loaded['second'] + loaded['third']
print(total)
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 C
Quick Check:
Save with names, load by names, sum elements [OK]
Hint: Save with names, load by names, sum with .sum() [OK]
Common Mistakes:
Using wrong keys when loading
Trying to sum arrays directly without .sum()
Saving arrays without names but accessing by names