【问题标题】:sklearn DeprecationWarning truth value of an arraysklearn DeprecationWarning 数组的真值
【发布时间】:2018-09-07 19:32:18
【问题描述】:

从文档中运行 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 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

【问题讨论】:

  • 我在 iPython 笔记本中尝试默认示例时遇到了同样的问题,this link 在我的案例中帮助抑制了警告。如果您还没有尝试过这个,也可能对您有所帮助!

标签: python scikit-learn rasa-nlu


【解决方案1】:

这个警告是由废弃 truth value check on empty array 的 numpy 引起的

此更改的理由是

不可能利用空数组为 False 的事实,因为数组可能由于其他原因为 False。

查看以下示例:

>>> 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 库上的 issue 10449,此问题已在库的 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),并且只想使弃用警告静音,那么我们可以通过使用 python 的Warningsfilterwarnings 方法来实现它模块

以下示例将产生上述问题中提到的弃用警告:

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:空数组的真值不明确。返回 False,但将来这将导致错误。使用array.size > 0 检查数组是否为空。

为了解决这个问题,为 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])

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

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

【讨论】:

  • +1 但不被接受为答案,因为它不能解决在不更改 numpy 版本的情况下消除错误
  • @rojobuffalo 我已经编辑了答案以显示如何忽略警告。我们可以使用 filterwarnings 方法来忽略任何类别的警告。
  • @GauravLuthra 出于某种原因这对我不起作用。它不断打印警告:(
  • @CarlosA.JimenezHolmquist,您能否分享一个您遇到问题的可重现示例?
【解决方案2】:

我也有同样的问题。以下解决方案对我有用。 转到警告中提到的路径文件和行号,如果您使用 anaconda,请转到 Anaconda\envs\py2\Lib\site-packages\sklearn\preprocessing\label.py 行号:151。

更改以下代码

if diff:
    raise ValueError("y contains new labels: %s" % str(diff))

到下面

if diff.size>0:
    raise ValueError("y contains new labels: %s" % str(diff))

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2020-10-13
    • 2019-07-21
    • 2020-11-06
    • 2019-10-08
    相关资源
    最近更新 更多