使用 sklearn 使用 KNeighborsClassifier 时出错

Posted

技术标签:

【中文标题】使用 sklearn 使用 KNeighborsClassifier 时出错【英文标题】:Error when using KNeighborsClassifier using sklearn 【发布时间】: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]

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

【问题讨论】:

【参考方案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 个样本。

以上是关于使用 sklearn 使用 KNeighborsClassifier 时出错的主要内容,如果未能解决你的问题,请参考以下文章

sh 使用没有root的acme.sh设置LetsEncrypt

使用sh startup.sh启动tomcat报错

在 platform.sh 上使用 SncRedisBundle

linux du -sh * 查看文件大小跟 du -sh出来的不一样

如何在不使用“sh”或“bash”命令的情况下运行 shell 脚本?

sh SH脚本截图并上传到Imgur(如果使用Snagit则从剪贴板获取)