【问题标题】:Error when using KNeighborsClassifier using sklearn使用 sklearn 使用 KNeighborsClassifier 时出错
【发布时间】:2017-09-20 10:34:05
【问题描述】:

我正在对一个包含 28 个特征和 5000 个样本的数据集进行 KNN 分类:

trainingSet = []
testSet = []
imdb_score = range(1,11)

print ("Start splitting the dataset ...")
splitDataset(path + 'movies.csv', 0.60, trainingSet, testSet)

print ("Start KNeighborsClassifier ... \n")
neigh = KNeighborsClassifier(n_neighbors=5)
neigh.fit(trainingSet, imdb_score)

但是,我遇到了这个错误:

    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [3362, 10]

我认为我的代码看起来不错。请问有没有人遇到过这个问题?

【问题讨论】:

    标签: numpy machine-learning scikit-learn knn


    【解决方案1】:

    所以你有 6000 个样本,使用其中的 60%,得到 3362 个样本(看起来,我没有为你的精确计算提供种子)。

    你打电话给fit(X,Y)where the following is needed:

    • y : {array-like, sparse matrix}
    • Target values of shape = [n_samples] or [n_samples, n_outputs]

    由于您的 y=imdb_score 只是 10 个值的列表,因此这些规则都不适用,因为它需要是具有 3362 个值的类似数组的数据结构(列表可以)或形状为 @987654326 的数组@。

    【讨论】:

    • 对不起,这是一个拼写错误。它是 5000 个样本。
    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2012-05-06
    • 2014-11-23
    • 2017-06-23
    • 2018-02-21
    • 2021-08-16
    相关资源
    最近更新 更多