0
0
NumPydata~20 mins

np.savez() for multiple arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.savez Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.savez() and np.load() for multiple arrays

What will be the output of the following code snippet?

import numpy as np

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

np.savez('data.npz', first=arr1, second=arr2)
loaded = np.load('data.npz')
print(list(loaded.keys()))
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)
loaded = np.load('data.npz')
print(list(loaded.keys()))
A['first', 'second']
B['data']
C['0', '1']
D['arr1', 'arr2']
Attempts:
2 left
💡 Hint

Think about how the arrays are named when saved with np.savez using keyword arguments.

data_output
intermediate
2:00remaining
Number of arrays saved in .npz file

Given the code below, how many arrays are stored inside the saved data.npz file?

import numpy as np

x = np.arange(5)
y = np.arange(5, 10)
z = np.arange(10, 15)

np.savez('data.npz', x=x, y=y, z=z)
NumPy
import numpy as np

x = np.arange(5)
y = np.arange(5, 10)
z = np.arange(10, 15)

np.savez('data.npz', x=x, y=y, z=z)
loaded = np.load('data.npz')
print(len(loaded.files))
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Count how many arrays are passed as named arguments to np.savez.

🔧 Debug
advanced
2:00remaining
Error when loading .npz file without closing

What error will occur when running this code?

import numpy as np

arr = np.array([1, 2, 3])
np.savez('file.npz', arr)
loaded = np.load('file.npz')
loaded.close()
print(loaded['arr'])
NumPy
import numpy as np

arr = np.array([1, 2, 3])
np.savez('file.npz', arr)
loaded = np.load('file.npz')
loaded.close()
print(loaded['arr'])
ANo error, prints [1 2 3]
BKeyError: 'arr'
CAttributeError: 'NpzFile' object has no attribute 'close'
DValueError: I/O operation on closed file.
Attempts:
2 left
💡 Hint

Consider what happens if you try to access data after closing the file object.

🚀 Application
advanced
2:00remaining
Saving multiple arrays with and without names

Which option correctly saves two arrays a and b into a single .npz file with keys 'a' and 'b' respectively?

NumPy
import numpy as np
a = np.array([10, 20])
b = np.array([30, 40])
Anp.savez('file.npz', a, b)
Bnp.savez('file.npz', {'a': a, 'b': b})
Cnp.savez('file.npz', a=a, b=b)
Dnp.savez('file.npz', ['a', a], ['b', b])
Attempts:
2 left
💡 Hint

Remember how keyword arguments work in Python functions.

🧠 Conceptual
expert
2:00remaining
Difference between np.savez() and np.savez_compressed()

Which statement best describes the difference between np.savez() and np.savez_compressed()?

A<code>np.savez_compressed()</code> saves arrays with compression, reducing file size but increasing save/load time.
B<code>np.savez()</code> compresses arrays automatically, while <code>np.savez_compressed()</code> does not.
CBoth functions save arrays without compression; the difference is in file format.
D<code>np.savez()</code> saves arrays as text files, <code>np.savez_compressed()</code> saves as binary.
Attempts:
2 left
💡 Hint

Think about compression and its effect on file size and speed.