天天看點

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)