sklearn DeprecationWarning 数组的真值 [英] sklearn DeprecationWarning truth value of an array

查看:37
本文介绍了sklearn DeprecationWarning 数组的真值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行文档中的 rasa_core 示例

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

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

.../sklearn/...: DeprecationWarning: 空数组的真值不明确.返回 False,但将来这将导致错误.使用`array.size >0` 来检查数组是否为空.

这是 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)>>>出口()

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

解决方案

这个警告是由 numpy 引起的,它弃用了 空数组的真值检查

此更改的理由是

<块引用>

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

检查以下示例:

<预><代码>>>>将 numpy 导入为 np>>>布尔(np.array([]))错误的>>># 但这不是测试空性的好方法,因为...>>>布尔(np.array([0]))错误的

解决方案

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

sudo pip install numpy==1.13.3

或使用 pip3 如下

sudo pip3 install numpy==1.13.3

忽略警告

如果我们想使用最新版本的库(在本例中为 numpy),它给出了弃用警告并且只想使弃用警告静音,那么我们可以使用 filterwarnings python 的警告 模块

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

from sklearn 导入预处理如果 __name__ == '__main__':le = 预处理.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 导入预处理进口警告如果 __name__ == '__main__':warnings.filterwarnings(action='ignore', category=DeprecationWarning)le = 预处理.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)

Running a rasa_core example from the docs with

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

and get this error output after each message in the dialog:

.../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.

It's an issue with numpy that has been fixed but not been published in the latest release: https://github.com/scikit-learn/scikit-learn/issues/10449

The following has not worked to temporarily silence the warning:

  1. Adding -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

解决方案

This warning is caused by numpy which deprecated the truth value check on empty array

Rationale for this change is

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

Check following example:

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

Solution

As per issue 10449 on scikit-learn library, this has been fixed in master branch of library. However that will be available around August 2018 so one possible alternate is to use a lesser version of numpy library which does not have this issue i.e. 1.13.3 since scikit-library by default would refer latest version of numpy(which is 1.14.2 at the time of writing this answer)

sudo pip install numpy==1.13.3

or with pip3 as follows

sudo pip3 install numpy==1.13.3

Ignoring the warning(s)

In case we want to use the latest version of library(numpy in this case) which is giving the deprecation warning and just want to silence the deprecation warning then we can achieve it by using filterwarnings method of python's Warnings module

Following example below would produce the deprecation warning mentioned in question above:

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

produces

/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.

And to take care of it, add filterwarnings for 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])

In case there are multiple modules which are giving warning and we want to selectively silent warning then use module attribute. e.g. to silent deprecation warning from scikit learn module

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

这篇关于sklearn DeprecationWarning 数组的真值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆