Issue
I recently encountered numpy.savez and numpy.savez_compressed. Both seem to work well with arrays of differing types, including object arrays. However, numpy.load does not work well with object type arrays. For example:
import numpy as np
numbers = np.full((10, 1), np.pi)
strings = np.full((10, 1), "letters", dtype=object)
np.savez("test.npz", numbers=numbers, strings=strings)
data = np.load("test.npz")
Calling data["strings"]
throws the following ValueError:
ValueError: Object arrays cannot be loaded when allow_pickle=False
However, enabling pickle on numpy.load
resolves this issue. Pickling is not discussed within the numpy.savez
and numpy.savez_compressed
documents...which makes me wonder why pickle is required to load the data. Do numpy.savez
and numpy.savez_compressed
use pickle automatically behind the scenes?
Solution
Since you have dtype=object
for that, pickle will be used (serializing Python objects).
Serializing with pickle is allowed by default when saving, but deserializing pickles must be explicitly requested when loading. That's because loading pickled data can execute arbitrary code, and for untrusted input this would be a security concern.
Answered By - wim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.