Reputation: 47
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.
Upvotes: 3
Views: 22614
Reputation: 140
To open a pickle file using Numpy, you just have to specify the flag "allow_pickle=True".
Here is an example that should match your issue:
Create example data:
import numpy as np
data = np.zeros([850,32,27])
Save example data as pickle file:
import pickle
file='data.pkl'
with open(file, 'wb') as file:
pickle.dump(data, file)
Load data using Numpy:
data_loaded = np.load(file,allow_pickle=True)
Check dimensions:
data_loaded.shape
BTW, as someone suggested, you can directly use pickle to load pickle file (that seems like the intuitive way):
Load data using Pickle:
with open(file, 'rb') as file:
data_loaded = pickle.load(file)
Upvotes: 0
Reputation: 129
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.
Upvotes: 6
Reputation: 491
You should load you array into a typle using the commands above, such as np.load('file', allow_pickle=True)
suggested by Steven, or call:
filein0 = open('tensor_name', "rb")
t0 = pickle.load(filein0)
t0 is a tuple {tensor name, numpy array}
Then, your required numpy array can be extracted using simple:
arr = t0[1]
Upvotes: 1
Reputation: 11657
The canonical way of doing this is:
import pickle
with open('my_array.pickle', 'rb') as file:
arr = pickle.load(file)
Upvotes: 0
Reputation: 3277
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])
Upvotes: 0