r - R : 'train and class have different lengths' ? 中的 KNN

标签 r

这是我的代码:

train_points <- read.table("kaggle_train_points.txt", sep="\t")
train_labels <- read.table("kaggle_train_labels.txt", sep="\t")
test_points <- read.table("kaggle_test_points.txt", sep="\t")

#uses package 'class'
library(class)
knn(train_points, test_points, train_labels, k = 5);
dim(train_points)是 42000 x 784dim(train_labels)是 42000 x 1

我没有看到问题,但我收到错误:

Error in knn(train_points, test_points, train_labels, k = 5) :
'train' and 'class' have different lengths.



有什么问题?

最佳答案

无法访问数据,真的很难提供帮助。但是,我怀疑 train_labels应该是一个向量。所以试试

cl = train_labels[,1]
knn(train_points, test_points, cl, k = 5)

还要仔细检查:
dim(train_points)
dim(test_points)
length(cl)

关于r - R : 'train and class have different lengths' ? 中的 KNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16276388/

相关文章:

r - 用绘图法保持尾随零

r - 有没有办法在 R 中返回操作系统编号格式

r - 调用 aictab 时未定义函数

r - plotly heatmapgl 中的分类轴

r - 计算两个日期之间的工作日

r - 使用索引迭代向量的子集

r - 使用 R 中的 ggplot2 按不同组对密度图进行分层

R:在两列和组中过滤两个 data.frames 中的重复值

r - glmnet NA/NaN/Inf 中的套索错误

r - 没有名为 'BiocInstaller' 的包