使用 sklearn 使用 KNeighborsClassifier 时出错

Error when using KNeighborsClassifier using sklearn

我正在对包含 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]

我认为我的代码看起来不错。请问,有人 运行 以前处理过这个问题吗?

所以你有 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 个值的类似数组的数据结构(列表可以)或数组形状 (3362, 1).