>> import pandas as pd >>> from sklearn import preprocessing, svm >>> df = -6ren">
gpt4 book ai didi

python - 将 sklearn 函数应用于 pandas 数据帧会给出 ValueError ("Unknown label type: %r"% y)

转载 作者:行者123 更新时间:2023-11-28 22:39:51 26 4
gpt4 key购买 nike

以下代码给出错误信息:

    >>> import pandas as pd
>>> from sklearn import preprocessing, svm
>>> df = pd.DataFrame({"a": [0,1,2], "b":[0,1,2], "c": [0,1,2]})
>>> clf = svm.SVC()
>>> df = df.apply(lambda x: preprocessing.scale(x))
>>> clf.fit(df[["a", "b"]], df["c"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Alexander\Anaconda\lib\site-packages\sklearn\svm\base.py", lin
151, in fit
y = self._validate_targets(y)
File "C:\Users\Alexander\Anaconda\lib\site-packages\sklearn\svm\base.py", lin
515, in _validate_targets
check_classification_targets(y)
File "C:\Users\Alexander\Anaconda\lib\site-packages\sklearn\utils\multiclass.
y", line 173, in check_classification_targets
raise ValueError("Unknown label type: %r" % y)
ValueError: Unknown label type: 0 -1.224745
1 0.000000
2 1.224745
Name: c, dtype: float64

pandas DataFrame 的 dtype 不是对象,所以应用 sklearn svm 函数应该没问题,但由于某些原因它无法识别分类标签。是什么导致了这个问题?

最佳答案

问题是在缩放步骤之后,标签是浮点值,这不是有效的标签类型;如果您转换为 intstr 它应该可以工作:

In [32]: clf.fit(df[["a", "b"]], df["c"].astype(int))
Out[32]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)

关于python - 将 sklearn 函数应用于 pandas 数据帧会给出 ValueError ("Unknown label type: %r"% y),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34346140/

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