"样本数量不一致" - scikit - 学习

时间:2017-07-30 12:54:27

标签: python machine-learning scikit-learn

我正在学习Python中机器学习的一些基础知识(scikit - learn),当我尝试实现K-最近邻算法时,会发生错误: ValueError:找到样本数不一致的输入变量:[426,143] 。我不知道如何处理它。
这是我的代码:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
cancer = load_breast_cancer()
X_train, y_train, X_test, y_test = train_test_split(cancer.data,cancer.target, 
                                                    stratify = 
                                                    cancer.target,
                                                    random_state = 0)
clf = KNeighborsClassifier(n_neighbors = 6)
clf.fit(X_train, y_train)`

1 个答案:

答案 0 :(得分:1)

train_test_splitX_train, X_test, y_train, y_test

的顺序返回一个元组

您已将返回值分配给错误的变量,因此您可以使用训练数据和测试数据,而不是训练数据和训练标签。

应该是

X_train, X_test, y_train, y_test = train_test_split()