sklearn中关于空数组的弃用错误,我的代码中没有任何空数组 [英] deprecation error in sklearn about empty array without any empty array in my code
问题描述
我只是在编码和解码,但是我从sklearn收到了这个错误:
I am just playing around encoding and decoding but I get this error from sklearn:
警告(来自警告模块): 文件"C:\ Python36 \ lib \ site-packages \ sklearn \ preprocessing \ label.py",第151行 如果差异: DeprecationWarning:空数组的真值不明确.返回False,但是将来会导致错误.使用
array.size > 0
检查数组是否为空.
Warning (from warnings module): File "C:\Python36\lib\site-packages\sklearn\preprocessing\label.py", line 151 if diff: 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.
这是完整的代码,您可以在python 3+中自己运行它
Here is the full code, you can run it yourself in python 3+
我的问题是为什么要说我使用空数组,因为我显然不在代码中,感谢您抽出宝贵时间回答我的问题.
My question is why is it saying I use an empty array as I clearly don't in my code, thanks for taking your time to answer my question.
### label encoding ###
import numpy as np
from sklearn import preprocessing
# Sample input labels
input_labels = ["red", "black", "red", "green",\
"black", "yellow", "white"]
# Create label encoder abd fit the label
encoder = preprocessing.LabelEncoder()
encoder.fit(input_labels)
# Print the mapping
print("\nLabel mapping:")
for i, item in enumerate(encoder.classes_):
print(item, "-->", i)
# Encode a set of labels using encoder
test_labels = ["green", "red", "black"]
encoded_values = encoder.transform(test_labels)
print("\nLabels =", test_labels)
print("Encoded values =", list(encoded_values))
# Decode a set of values using the encoder
encoded_values = [3, 0, 4, 1]
decoded_list = encoder.inverse_transform(encoded_values)
print("\nEncoded values =", encoded_values)
print("Decoded labels=", list(decoded_list))
推荐答案
TLDR:您可以忽略该警告.这是由于sklearn在内部进行的操作不理想所致.
TLDR: You can ignore the warning. It is caused by sklearn doing something internally that is not quite ideal.
该警告实际上是由numpy
引起的,该在空数组上弃用了真相测试:
The warning is actually caused by numpy
, which deprecated truth testing on empty arrays:
总而言之,对空数组进行真相测试是危险的,令人误解的,并且没有任何用处,因此不建议使用.
The long and short is that truth-testing on empty arrays is dangerous, misleading, and not in any way useful, and should be deprecated.
这意味着不应执行if array:
之类的操作来检查array
是否为空.但是,sklearn 在0.19.1版本:
This means one is not supposed to do something like if array:
to check if array
is empty. However, sklearn does this in the 0.19.1 release:
diff = np.setdiff1d(y, np.arange(len(self.classes_)))
if diff:
raise ValueError("y contains new labels: %s" % str(diff))
因为您的numpy版本最近,它会抱怨并发出警告.
Because your version of numpy is recent enough it complains and issues a warning.
在sklearn的当前主分支中,问题已得到解决我希望该修复程序将包含在下一个版本中.
The problem has been fixed in sklearn's current master branch, so I'd expect the fix to be included in the next release.
这篇关于sklearn中关于空数组的弃用错误,我的代码中没有任何空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!