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])
Share:
11,135

Related videos on Youtube

user8863227
Author by

user8863227

Updated on June 04, 2022

Comments

  • user8863227
    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
      timedacorn over 5 years
      you can use pickle.load() to load the pickle file
  • Julien
    Julien over 5 years
    This doesn't answer the question. The array is apparently already saved with pickle and needs to be loaded.
  • Stein
    Stein over 5 years
    It's the proper way of doing it.
  • Julien
    Julien over 5 years
    Maybe but if you don't have a time machine you got to do with what you have.
  • Josh Friedlander
    Josh Friedlander over 5 years
    I like answers that suggest good practice rather than helping OP do something the wrong way :) +1
  • user10556443
    user10556443 over 3 years
    The 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
    user10556443 over 3 years
    The 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
    user10556443 over 3 years
    The 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