Python Question

What causes the unpickling error in numpy?

I'm currently getting an unpickling error for loading a gz file using the numpy load function. I'm not sure what's causing this. Could offer some suggestions. I'm not sure if the data inside the file might be the problem.

import numpy as np
import gzip
import io
import pickle


n = np.load("proteins1.npy.gz",mmap_mode='r')
print(n)


Here is the error:

UnpicklingError Traceback (most recent call last)
C:\Users\Aivan Francisco\Anaconda3\lib\site-packages\numpy\lib\npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
412 try:
--> 413 return pickle.load(fid, **pickle_kwargs)
414 except:


UnpicklingError:

During handling of the above exception, another exception occurred:

OSError Traceback (most recent call last)
<ipython-input-1-b9f88486c866> in <module>()
7 #file_name = open("proteins1.npy.gz","r")
8


----> 9 n = np.load("proteins1.npy.gz",mmap_mode='r')
10 print(n)
11

C:\Users\Aivan Francisco\Anaconda3\lib\site-packages\numpy\lib\npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
414 except:
415 raise IOError(
--> 416 "Failed to interpret file %s as a pickle" % repr(file))
417 finally:
418 if own_fid:

OSError: Failed to interpret file 'proteins1.npy.gz' as a pickle

Answer

I believe that numpy.load expects to read an uncompressed *.npy file, not a gzipped *.npy.gz file. Try uncompressing the file first before loading it.

Comments