Python-->;sklearn-->;预处理-->;标签编码:错误
我运行以下代码对标签进行编码和解码,而我的终端显示错误: if diff:DeprecationWarning:空数组的真值不明确。返回False,但在将来这将导致错误。使用Python-->;sklearn-->;预处理-->;标签编码:错误,python,Python,我运行以下代码对标签进行编码和解码,而我的终端显示错误: if diff:DeprecationWarning:空数组的真值不明确。返回False,但在将来这将导致错误。使用array.size>0检查数组是否为空 from sklearn import preprocessing as pp import numpy as np decode_values=['a','b','c','d','e','f','g'] encoder=pp.LabelEncoder() encoder.fit(
array.size>0
检查数组是否为空
from sklearn import preprocessing as pp
import numpy as np
decode_values=['a','b','c','d','e','f','g']
encoder=pp.LabelEncoder()
encoder.fit(decode_values)
print(encoder.inverse_transform(np.array([0,1])))
我打开label.py并获取导致错误的代码:
def inverse_transform(self, y):
"""Transform labels back to original encoding.
Parameters
----------
y : numpy array of shape [n_samples]
Target values.
Returns
-------
y : numpy array of shape [n_samples]
"""
check_is_fitted(self, 'classes_')
diff = np.setdiff1d(y, np.arange(len(self.classes_)))
if diff:
raise ValueError("y contains new labels: %s" % str(diff))
y = np.asarray(y)
return self.classes_[y]
在我的例子中,变量“diff”应该是空的,但是机器无法理解“if diff”。在我将其更改为“if list(diff)”之后,错误消失了。我检查了我的课本和其他视频,这个错误没有发生。我对这种差异感到困惑
DeprecationWarning
告诉您为什么会出现这种情况。如果diff,则不能使用,因为此时diff
将返回False
,然后将引发错误。“空数组的真值不明确”是不言自明的。当您使用list(diff)
时,这将生成一个空列表,其真值不含糊。但是,如果diff.size>0,您应该使用,正如弃用警告中所建议的那样。我需要学习如何读取错误消息。谢谢