sklearn DeprecationWarning数组的真值

2024-05-23 16:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

运行文档中的rasa_核心示例

› python3 -m rasa_core.run -d models/dialogue -u models/nlu/default/current

并在对话框中的每条消息之后获取此错误输出:

.../sklearn/...: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.

这是一个关于numpy的问题,该问题已被修复,但尚未在最新版本中发布:https://github.com/scikit-learn/scikit-learn/issues/10449

以下操作未起作用,无法暂时关闭警告:

  1. 添加-W ignore

python3 -W ignore -m rasa_core.run -d models/dialogue -u models/nlu/default/current

  1. warnings.simplefilter

python3

>>> warnings.simplefilter('ignore', DeprecationWarning)
>>> exit()

python3 -m rasa_core.run -d models/dialogue -u models/nlu/default/current


Tags: runcoreandefaultismodelscurrentarray
1条回答
网友
1楼 · 发布于 2024-05-23 16:21:40

此警告是由于numpy不赞成使用truth value check on empty array

这种改变的理由是

It is impossible to take advantage of the fact that empty arrays are False, because an array can be False for other reasons.

检查以下示例:

>>> import numpy as np
>>> bool(np.array([]))
False
>>> # but this is not a good way to test for emptiness, because...
>>> bool(np.array([0]))
False

解决方案

根据scikit learn library上的issue 10449,此问题已在library的master分支中修复。但是,这将在2018年8月左右提供,因此一个可能的替代方案是使用不存在此问题的较低版本的numpy库,即1.13.3,因为scikit库默认将引用最新版本的numpy(在编写此答案时为1.14.2)

sudo pip install numpy==1.13.3

或者使用pip3,如下所示

sudo pip3 install numpy==1.13.3

忽略警告

如果我们想使用最新版本的库(在本例中是numpy),它给出了deprecation警告,并且只想让deprecation警告保持沉默,那么我们可以使用python的filterwarnings模块的Warnings方法来实现它

以下示例将生成上述问题中提到的折旧警告:

from sklearn import preprocessing

if __name__ == '__main__':
    le = preprocessing.LabelEncoder()
    le.fit([1, 2, 2, 6])
    le.transform([1, 1, 2, 6])
    le.inverse_transform([0, 0, 1, 2])

产生

/usr/local/lib/python2.7/dist-packages/sklearn/preprocessing/label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use array.size > 0 to check that an array is not empty.

为了解决这个问题,添加filterwarning作为DeprecationWarning

from sklearn import preprocessing
import warnings

if __name__ == '__main__':
    warnings.filterwarnings(action='ignore', category=DeprecationWarning)
    le = preprocessing.LabelEncoder()
    le.fit([1, 2, 2, 6])
    le.transform([1, 1, 2, 6])
    le.inverse_transform([0, 0, 1, 2])

如果有多个模块发出警告,并且我们希望选择性地发出无声警告,则使用模块属性。e、 g.从scikit学习模块发出无声弃用警告

warnings.filterwarnings(module='sklearn*', action='ignore', category=DeprecationWarning)

相关问题 更多 >