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()))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()))
Think about how the arrays are named when saved with np.savez using keyword arguments.
When using np.savez with named arguments, the keys in the loaded file correspond to those names. Here, the arrays are saved with keys 'first' and 'second'.
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)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))
Count how many arrays are passed as named arguments to np.savez.
Three arrays are saved with keys 'x', 'y', and 'z', so the file contains 3 arrays.
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'])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'])
Consider what happens if you try to access data after closing the file object.
After calling loaded.close(), the file is closed. Accessing loaded['arr'] tries to read from a closed file, causing a ValueError.
Which option correctly saves two arrays a and b into a single .npz file with keys 'a' and 'b' respectively?
import numpy as np a = np.array([10, 20]) b = np.array([30, 40])
Remember how keyword arguments work in Python functions.
Using keyword arguments like a=a and b=b assigns keys 'a' and 'b' in the saved file. Other options are invalid syntax or incorrect usage.
Which statement best describes the difference between np.savez() and np.savez_compressed()?
Think about compression and its effect on file size and speed.
np.savez_compressed() compresses the arrays to reduce file size but this can make saving and loading slower. np.savez() saves without compression.