【问题标题】:"Inconsistent numbers of samples" - scikit - learn“样本数量不一致” - scikit - 学习
【发布时间】:2017-07-30 12:54:26
【问题描述】:

我正在使用 Python (scikit - learn) 学习机器学习的一些基础知识,当我尝试实现 K-最近邻算法时,出现错误:ValueError: Found input variables with contrast numbers of samples: [ 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)`

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    train_test_splitX_train, X_test, y_train, y_test的顺序返回一个元组

    您将返回值分配给了错误的变量,因此您正在拟合训练数据和测试数据,而不是训练数据和训练标签。

    应该是

    X_train, X_test, y_train, y_test = train_test_split()
    

    【讨论】:

    • 原来如此简单..我感到很惭愧。谢谢:)
    猜你喜欢
    • 2016-05-28
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2018-10-21
    • 2016-12-20
    • 2022-08-14
    相关资源
    最近更新 更多