ValueError:未知标签类型:使用cross_validation时

时间:2017-01-21 11:52:20

标签: python python-3.x machine-learning cross-validation

以下是代码:

import pandas as pd
import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv',index_col = 0)
X = data[['TV','Radio','Newspaper']]
y = data[['Sales']]
y = np.asarray(y)
y = np.ravel(y)
knn = KNeighborsClassifier(n_neighbors = 5)
scores = cross_val_score(knn,X,y,cv=10,scoring = 'accuracy')
print(scores)

我收到以下错误

C:\Users\Kunal Desai\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
171     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput', 
172             'multilabel-indicator', 'multilabel-sequences']:
--> 173         raise ValueError("Unknown label type: %r" % y)
174 
175 

ValueError: Unknown label type:

我是cross_validation和scikit-learn的新手

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果要预测连续变量,则需要回归而不是分类。 KNeighborsRegressor与KNeighborsClassifier。