如何解决python VisibleDeprecationWarning
导入数据集时遇到了以下错误。
from keras.datasets import reuters
(train_data,train_labels),(test_data,test_labels) = reuters.load_data(num_words=10000)
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
(which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this,you must specify 'dtype=object' when creating the ndarray
x_test,y_test = np.array(xs[idx:]),np.array(labels[idx:])
解决方法
此警告来自 numpy.array()
函数,当 numpy 尝试使用不同长度的嵌套序列创建数组时出现。要调用警告,您可以执行以下操作:
import numpy as np
labels = ['a','b','c']
xs = [[1,2],[4],[2,5,7]]
(train,test) = (np.array(xs[:2]),np.array(labels[:2]))
那么你肯定会得到 VisibleDeprecationWarning
由 xs
数组引起的。当您将 xs
更改为 xs=[[1,[4,7],5]]
时,警告将消失。同样的情况来自路透社的数据——有许多嵌套的数组,但长度各不相同。顺便说一句,您可以通过使用 xs
的 maxlen
参数对 load_data()
数组执行相同的操作,并将其设置为路透社数据中最短的嵌套数组,如下所示:{{1 }}...这显然是你不想做的事情。
所以,最简单的解决方案就是关闭警告,因为...嘿!这只是一个警告:)
reuters.load_data(maxlen=13)
另一种解决方案是修改 import warnings
import numpy as np
from keras.datasets import reuters
warnings.simplefilter(action='ignore',category=np.VisibleDeprecationWarning)
(train_data,train_labels),(test_data,test_labels) = reuters.load_data(num_words=10000)
函数,其中 reuters.load_data()
调用中缺少 dtype=object
(最终您可以在 np.array()
中使用 action='error'
- 那么错误是引发而不是警告 - 在 warnings.simplefilter
块中运行 load_data()
并在捕获错误时调用修改后的 reuters 模块...)
try-except
但我想关闭警告就足够了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。