找到具有不一致样本数的输入变量:Python中的[23038,7680]

时间:2018-04-23 18:18:02

标签: python scikit-learn classification knn

我尝试运行以下代码:

from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors =5, metric = 'minkowski', p = 2 )
classifier.fit(X_train, y_test)

我得到的错误是:

from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors =5, metric = 'minkowski', p = 2 )
classifier.fit(X_train, y_test)
Traceback (most recent call last):

File "<ipython-input-47-60c527e5ec72>", line 3, in <module>
classifier.fit(X_train, y_test)

File "C:\Users\rahul\Anaconda3\lib\site-packages\sklearn\neighbors\base.py", line 765, in fit
X, y = check_X_y(X, y, "csr", multi_output=True)

File "C:\Users\rahul\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 583, in check_X_y
check_consistent_length(X, y)

File "C:\Users\rahul\Anaconda3\lib\site-pac kages\sklearn\utils\validation.py", line 204, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])

ValueError: Found input variables with inconsistent numbers of samples: [23038, 7680]

任何人都知道我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:2)

您正在使用错误的标签(即测试标签)进行训练测试。请使用培训标签

from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors =5, metric = 'minkowski', p = 2 )
classifier.fit(X_train, y_train)
classifier.predict(X_test)