gpt4 book ai didi

arrays - sklearn中关于空数组的弃用错误在我的代码中没有任何空数组

转载 作者:行者123 更新时间:2023-12-03 23:21:31 25 4
gpt4 key购买 nike

我只是在玩编码和解码,但我从 sklearn 得到这个错误:

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+中自己运行它

我的问题是为什么它说我使用了一个空数组,因为我的代码中显然没有,感谢您花时间回答我的问题。
### 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 在内部做了一些不太理想的事情引起的。

该警告实际上是由 numpy 引起的,其中 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 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 版本足够新,所以它会提示并发出警告。

sklearn 当前主分支中的 has been fixed 问题,因此我希望该修复程序包含在下一个版本中。

关于arrays - sklearn中关于空数组的弃用错误在我的代码中没有任何空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48687375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com