how to load pickle file to numpy array
11,135
Solution 1
You can load pickled objects with np.load()
, setting the parameter allow_pickle
to True
.
np.load('PATH_TO_PICKLE_FILE', allow_pickle=True)
For more info check this.
Solution 2
Numpy has a
np.save()
https://docs.scipy.org/doc/numpy/reference/generated/numpy.save.html
and a
np.load()
https://docs.scipy.org/doc/numpy/reference/generated/numpy.load.html#numpy.load
routine specifically for this purpose. Using pickle is possible, but it seems like over kill.
In [1]: import numpy as np
In [2]: A = np.linspace(0,1,10)
In [3]: np.save("bla.npy",A)
In [4]: B = np.load("bla.npy")
In [5]: B == A
Out[5]:
array([ True, True, True, True, True, True, True, True, True,
True])
Related videos on Youtube
Author by
user8863227
Updated on June 04, 2022Comments
-
user8863227 about 2 years
I saved Numpy array to pickle file.The shape was (850,32,27).How can I load this pickle file to Numpy array? I tried to look up answer ,however I could not fine any.
-
timedacorn over 5 yearsyou can use
pickle.load()
to load the pickle file
-
-
Julien over 5 yearsThis doesn't answer the question. The array is apparently already saved with pickle and needs to be loaded.
-
Stein over 5 yearsIt's the proper way of doing it.
-
Julien over 5 yearsMaybe but if you don't have a time machine you got to do with what you have.
-
Josh Friedlander over 5 yearsI like answers that suggest good practice rather than helping OP do something the wrong way :) +1
-
user10556443 over 3 yearsThe question was how to extract back the numpy array from the saved pickle file, which it load operation returns a tuple of name and numpy, so required answer would be of the way to extract the numpy array from the tuple
-
user10556443 over 3 yearsThe question was how to extract back the numpy array from the saved pickle file, which it load operation returns a tuple of name and numpy, so required answer would be of the way to extract the numpy array from the tuple
-
user10556443 over 3 yearsThe question was how to extract back the numpy array from the saved pickle file, which it load operation returns a tuple of name and numpy, so required answer would be of the way to extract the numpy array from the tuple