Bird
Raised Fist0
NumPydata~10 mins

np.savez() for multiple arrays in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - np.savez() for multiple arrays
Prepare arrays to save
↓
Call np.savez(filename, arrays...)
↓
Arrays packed into a .npz file
↓
File saved on disk
↓
Later: Load .npz file with np.load()
↓
Access arrays by keys
↓
Use arrays in your program
You prepare arrays, save them together in one .npz file, then load and access them later by name.
Execution Sample
NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

np.savez('data.npz', first=arr1, second=arr2)
This code saves two arrays named 'first' and 'second' into one file called 'data.npz'.
Execution Table
StepActionInput ArraysFile CreatedOutput
1Create arr1[1, 2, 3]Noarr1 ready
2Create arr2[4, 5, 6]Noarr2 ready
3Call np.savez('data.npz', first=arr1, second=arr2)[arr1, arr2]YesFile 'data.npz' with arrays saved
4Load file with np.load('data.npz')File 'data.npz'NoLoaded file object with keys 'first', 'second'
5Access arrays by keysLoaded fileNoArrays retrieved: first=[1 2 3], second=[4 5 6]
💡 All arrays saved and accessible from the .npz file.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
arr1None[1 2 3][1 2 3][1 2 3][1 2 3][1 2 3]
arr2NoneNone[4 5 6][4 5 6][4 5 6][4 5 6]
file 'data.npz'Not createdNot createdCreatedCreatedLoadedLoaded
loaded_fileNoneNoneNoneNoneFile objectFile object
loaded_file['first']NoneNoneNoneNoneNone[1 2 3]
loaded_file['second']NoneNoneNoneNoneNone[4 5 6]
Key Moments - 3 Insights
Why do we give names like 'first' and 'second' when saving arrays?
Because np.savez saves arrays with keys. These names become keys to access arrays later, as shown in execution_table step 5.
What happens if we don't give names to arrays in np.savez()?
Arrays get default names like 'arr_0', 'arr_1'. You can still access them by these keys after loading.
Is the .npz file human-readable?
No, it's a compressed binary file storing arrays efficiently. You must load it with np.load to read arrays.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is created?
AA new array in memory
BA text file with array data
CA file named 'data.npz' containing arrays
DAn error because arrays are unnamed
💡 Hint
Check the 'File Created' and 'Output' columns at step 3 in execution_table.
According to variable_tracker, what is the value of loaded_file['second'] after step 5?
A[4 5 6]
BNone
C[1 2 3]
DFile object
💡 Hint
Look at the last column for loaded_file['second'] in variable_tracker.
If we omit names in np.savez(arr1, arr2), how do we access the second array after loading?
Aloaded_file['second']
Bloaded_file['arr_1']
Cloaded_file[1]
Dloaded_file['arr2']
💡 Hint
Recall key naming defaults mentioned in key_moments about unnamed arrays.
Concept Snapshot
np.savez(filename, **arrays) saves multiple arrays in one .npz file.
Each array is saved with a key (name).
Load with np.load(filename) to get a file object.
Access arrays by keys like loaded['keyname'].
Useful to store and share multiple arrays together.
Full Transcript
This visual execution shows how np.savez saves multiple numpy arrays into a single .npz file. First, arrays are created in memory. Then np.savez is called with a filename and named arrays. This creates a compressed file storing all arrays with their keys. Later, np.load reads the file and returns an object to access arrays by their keys. The execution table traces each step from array creation, saving, loading, to accessing arrays. The variable tracker shows how variables and file states change. Key moments clarify why naming arrays is important and that .npz files are binary. The quiz tests understanding of file creation, array access, and default naming. The snapshot summarizes usage in a few lines.

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

  1. Step 1: Understand the function purpose

    np.savez() is used to save multiple arrays into one file, making storage and sharing easier.
  2. Step 2: Compare options with function use

    Loading arrays is done by np.load(), not np.savez(). Creating or deleting arrays is unrelated.
  3. Final Answer:

    To save multiple arrays into a single file for easy storage -> Option A
  4. 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

  1. Step 1: Recall named saving syntax

    To save arrays with names, use np.savez(filename, name1=array1, name2=array2).
  2. 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.
  3. Final Answer:

    np.savez('data.npz', first=a, second=b) -> Option D
  4. 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

  1. Step 1: Understand default naming in np.savez()

    If arrays are saved without names, NumPy assigns default keys like 'arr_0', 'arr_1', etc.
  2. 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.
  3. Final Answer:

    ['arr_0', 'arr_1'] -> Option A
  4. 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

  1. Step 1: Check how arrays were saved

    Arrays x and y were saved without names, so keys are 'arr_0' and 'arr_1'.
  2. 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.
  3. Final Answer:

    KeyError because 'x' was not saved with a name -> Option B
  4. 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

  1. 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.
  2. 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.
  3. 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
  4. 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