ValueError: Object arrays cannot be loaded when allow_pickle=False解决方案

在我读.npz文件时报下面错误:

#读取.npz 数据
population_data=np.load("./data/populations.npz")
print(population_data.files)#里面有两个数组   data  feature_names
data=population_data['data']
print(data)
print(population_data['feature_names'])

报错:

['data', 'feature_names']
Traceback (most recent call last):
  File "E:/pycharm file/使用scikit-learn构建模型/构建一元线性模型.py", line 32, in 
    data=population_data['data']
  File "E:\pycharm file\venv\lib\site-packages\numpy\lib\npyio.py", line 262, in __getitem__
    pickle_kwargs=self.pickle_kwargs)
  File "E:\pycharm file\venv\lib\site-packages\numpy\lib\format.py", line 692, in read_array
    raise ValueError("Object arrays cannot be loaded when "
ValueError: Object arrays cannot be loaded when allow_pickle=False

报错为:numpy版本太高,我用的是1.16.3,应该降级为1.16.2

两种解决方案:

Numpy 1.16.3几天前发布了。从发行版本中说明:“函数np.load()和np.lib.format.read_array()采用allow_pickle关键字,现在默认为False以响应CVE-2019-6446 < nvd.nist.gov/vuln/detail / CVE-2019-6446 >“。降级到1.16.2对我有帮助,因为错误发生在一些library内部
第一种:点击报错处,进入源代码(.py),注释掉693行:

#if not allow_pickle:
    #raise ValueError("Object arrays cannot be loaded when "
                   #  "allow_pickle=False")

    # Now read the actual data.
    if dtype.hasobject:
        # The array contained Python objects. We need to unpickle the data.
        #if not allow_pickle:
            #raise ValueError("Object arrays cannot be loaded when "
                           #  "allow_pickle=False")
        if pickle_kwargs is None:
            pickle_kwargs = {}
        try:
            array = pickle.load(fp, **pickle_kwargs)
        except UnicodeError as err:
            if sys.version_info[0] >= 3:
                # Friendlier error message

修改后成功解决了问题,但改掉源码不知道会不会有后遗症

第二种:降级numpy版本

pip install numpy==1.16.2

上述两种方法我都尝试了,成功解决了报错问题

你可能感兴趣的:(Python,3,程序报错解决方案)