python - 数组的 sklearn DeprecationWarning 真值

标签 python scikit-learn rasa-nlu

使用文档运行 rasa_core 示例

› 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忽略

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

最佳答案

此警告是由 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

解决方案

根据 issue 10449在 scikit-learn 库中,这已在库的 master 分支中修复。然而,它将在 2018 年 8 月左右可用,因此一种可能的替代方法是使用没有此问题的较小版本的 numpy 库,即 1.13.3,因为默认情况下 scikit-library 会引用最新版本的 numpy(1.14.2 在写这个答案的时间)

sudo pip install numpy==1.13.3

或者使用 pip3 如下

sudo pip3 install numpy==1.13.3

忽略警告

如果我们想使用给出弃用警告的最新版本的库(在本例中为 numpy)并且只想使弃用警告静音,那么我们可以使用 filterwarnings 来实现它python的方法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.

为了处理它,为 DeprecationWarning 添加 filterwarnings

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])

如果有多个模块发出警告,而我们想要有选择地静音警告,则使用 module 属性。例如来自 scikit 学习模块的静默弃用警告

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

关于python - 数组的 sklearn DeprecationWarning 真值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49545947/

相关文章:

python - 使用 Python 2.7.x 在 Windows 8.1 上安装 NumPy

python - 在 gridsearchcv sklearn 中提供训练和验证集

rasa-nlu - 如何从命令行与 rasa 助手交互?

python-2.7 - RASA 没有识别出正确的 Intent

nlp - RASA NLU 或 SNIPS NLU 哪个最好?

Python 和 Tkinter : Function madness

python - 给定所述元组中的项目,获取浅元组/列表的索引

python - 来自 pandas 数据框的输入的 sklearn classification_report 产生 : "TypeError: not all arguments converted during string formatting"

python - 在线/流媒体学习的验证

python - Scikit Learn K-means 聚类和 TfidfVectorizer : How to pass top n terms with highest tf-idf score to k-means