我正在学习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)`
答案 0 :(得分:1)
train_test_split
按X_train, X_test, y_train, y_test
您已将返回值分配给错误的变量,因此您可以使用训练数据和测试数据,而不是训练数据和训练标签。
应该是
X_train, X_test, y_train, y_test = train_test_split()