Python DeprecationWarning: The truth value of an empty array is ambiguous. Returning False

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


经过在网上查找问题发现:这是一个numpy问题,已经修复,但未在最新版本中发布:https//github.com/scikit-learn/scikit-learn/issues/10449

>>> import numpy as np
>>> bool(np.array([]))
False
>>> import numpy as np
>>> bool(np.array([0]))
False



忽略警告(S)

如果我们想要使用最新版本的库(在这种情况下为numpy),它提供了弃用警告,并且只想静音弃用警告,那么我们可以通过使用python的Warnings模块的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])

如果有多个模块正在发出警告,并且我们希望选择性地进行无声警告,请那么使用模块属性。例如从scikit学习模块发出静默弃用警告

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


猜你喜欢

转载自blog.csdn.net/u010899985/article/details/80514316
今日推荐