当前位置: 首页 > 工具软件 > typer > 使用案例 >

sklearn.svm.SVC中raise ValueError(“Unknown label type: %r” % y_type)ValueError: Unknown label type:处理

茹照
2023-12-01

关于sklearn.svm.SVC中raise ValueError(“Unknown label type: %r” % y_type)ValueError: Unknown label type: 'unknown’报错的处理,标签列除了整型数据还可以是字符型

报错内容如下:

Traceback (most recent call last):
  File "/home/python/Desktop/桥/knn算法.py", line 137, in <module>
    clf = SVC(kernel="rbf", gamma=2947.0517025518097).fit(train_data, labels)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/svm/_base.py", line 149, in fit
    y = self._validate_targets(y)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/svm/_base.py", line 525, in _validate_targets
    check_classification_targets(y)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/utils/multiclass.py", line 169, in check_classification_targets
    raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'unknown'

Process finished with exit code 1

查看标签列数据类型

print(labels.dtype)

输出object,此时SVC.fit()就会报以上错误

解决办法

#如果标签列要求使用浮点型,fit()会报错,此时可以把数据类型转换成'str'
labels = df.iloc[1:91, 2].astype("str").values
#同样转换成'int'也可以
labels = df.iloc[1:91, 2].astype("int").values
 类似资料: