ValueError: Unknown label type: 'unknown'-Labels are numeric

ValueError: Unknown label type: 'unknown'-Labels are numeric

我正在构建一个用于二元分类的随机森林分类器problem.My标签都是数字。

print labels.unique()
[1 0]

print type(labels)
    <class 'pandas.core.series.Series'>
print labels.shape
(3000,)

但是当我用 Gridsearchcv 拟合模型时

pipeline = Pipeline(steps=[('scaler', scaler), ('algorithm', algo)])
cv = StratifiedShuffleSplit(labels, 5, test_size=0.25, random_state=42)
gs = GridSearchCV(pipeline, param_grid, cv=cv, scoring='f1')
gs.fit(features, labels)

我遇到了这个错误

ValueError: Unknown label type: 'unknown'

但是当我使用

gs.fit(features, labels.astype(int))

它正在工作fine.Can有人告诉我标签中的问题出在哪里吗?

您只需使用 tolist() 方法更改要列出的标签类型。使用

labels_lst = labels.tolist()

Scikit-learn 无法自动将系列转换为标签列表。