I save an object of my class by numpy.save() and I can load it by numpy.load() but the value that I retrieve is: [<classes.SampleClass object at 0x7fb8ebd7a1f0>].
class SampleClass:
def __init__(self, y):
self.y = y
y=np.array([1,2,3])
x=SampleClass(y)
np.save("x",x, allow_pickle=True)
xx=np.load("x.npy")
This code is an example and xx
value is [<classes.SampleClass object at 0x7fb8ebd7a1f0>]. How can I access to the values of y
(xx.y
)?
CodePudding user response:
'allow_pickle' is required for the load
, not the save
.
In [2]: class SampleClass:
...:
...: def __init__(self, y):
...: self.y = y
...:
...:
...: y=np.array([1,2,3])
...: x=SampleClass(y)
...: np.save("x",x)
...: xx=np.load("x.npy", allow_pickle=True)
In [2]:
In [3]: xx
Out[3]: array(<__main__.SampleClass object at 0x000002583B8BCDC0>, dtype=object)
xx
is a 0d object dtype array. It has one element, which can be extracted with
In [4]: xx.item()
Out[4]: <__main__.SampleClass at 0x2583b8bcdc0>
or:
In [5]: xx[()]
Out[5]: <__main__.SampleClass at 0x2583b8bcdc0>
The ()
index matches the 0d shape of the array.