Object arrays cannot be loaded when allow_pickle=False – Code Example

Total
0
Shares

In this article I will provide you Python code to resolve ValueError: Object arrays cannot be loaded when allow_pickle=False. We cannot use numpy.load() function to load object arrays without pickle module.

According to Numpy documentation

Loading files that contain object arrays uses the pickle module, which is not secure against erroneous or maliciously constructed data. Consider passing allow_pickle=False to load data that is known not to contain object arrays for the safer handling of untrusted sources.

If you are not loading object arrays then its advised to use allow_pickle=false. But for object arrays, allow_pickle needs to be true.

Load arrays or pickled objects from .npy.npz or pickled files.

The format of numpy.load() is –

numpy.load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, encoding='ASCII')

Code Example

Error Code – Let’s first replicate the error –

from keras.datasets import imdb

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

It will throw ValueError: Object arrays cannot be loaded when allow_pickle=False

Traceback (most recent call last)
<ipython-input-1-2ab3902db485> in <module>()
      1 from keras.datasets import imdb
----> 2 (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
ValueError: Object arrays cannot be loaded when allow_pickle=False

Solution

import numpy as np
# save np.load
np_load_old = np.load

# modify the default parameters of np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)

# call load_data with allow_pickle implicitly set to true
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

# restore np.load for future normal usage
np.load = np_load_old