Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R knn和x27中的错误;列车';和';类别';长度不同_R_Nearest Neighbor_Knn - Fatal编程技术网

R knn和x27中的错误;列车';和';类别';长度不同

R knn和x27中的错误;列车';和';类别';长度不同,r,nearest-neighbor,knn,R,Nearest Neighbor,Knn,我试图在我的数据集上使用knn函数(来自类包)。它有5列特性,第6列是我希望能够预测的。我在做70/30分割 这是我的密码: > ind <- createDataPartition(CSD$Caesarian, p=0.70, list=FALSE) > csd_train <- CSD[ ind,] > csd_test <- CSD[-ind,] > c1 <- CSD[1:6,-c(1,2,3,4,5)] > knn(train, t

我试图在我的数据集上使用knn函数(来自类包)。它有5列特性,第6列是我希望能够预测的。我在做70/30分割

这是我的密码:

> ind <- createDataPartition(CSD$Caesarian, p=0.70, list=FALSE)
> csd_train <- CSD[ ind,]
> csd_test <- CSD[-ind,]
> c1 <- CSD[1:6,-c(1,2,3,4,5)]
> knn(train, test, c1, k=2, prob=TRUE)
我查看了其他线程并尝试了他们建议的解决方案()

并尝试了以下操作,但仍有错误

> c1 = as.factor(c1)
> dim(csd_train)
[1] 57  6
> dim(csd_test)
[1] 23  6
> length(c1)
[1] 6
> knn(train, test, c1, k=2, prob=TRUE)
Error in knn(train, test, c1, k = 2, prob = TRUE) : 
  'train' and 'class' have different lengths
我也试过了,但还是出错了

> c1 = as.factor(CSD[['Caesarian']])
> knn(train, test, c1, k=2, prob=TRUE)
Error in knn(train, test, c1, k = 2, prob = TRUE) : 
  'train' and 'class' have different lengths
我不知道如何解决这个问题

以下是我的数据样本,如果有帮助的话:

> dput(head(CSD))
structure(list(Age = c(22L, 26L, 26L, 28L, 22L, 26L), Delivery.NO = c(1L, 
2L, 2L, 1L, 2L, 1L), Delivery.NO.1 = c(1L, 1L, 0L, 1L, 1L, 0L
), BP = c(2L, 1L, 1L, 2L, 1L, 0L), Heart.Problem = c(1L, 1L, 
1L, 1L, 1L, 1L), Caesarian = structure(c(1L, 2L, 1L, 1L, 2L, 
1L), .Label = c("N", "Y"), class = "factor")), .Names = c("Age", 
"Delivery.NO", "Delivery.NO.1", "BP", "Heart.Problem", "Caesarian"
), row.names = c(NA, 6L), class = "data.frame")
编辑 是的


我所有的预测变量都是数值,没有遗漏值。

我想我有一个答案

下面是一个使用
iris
数据集的工作示例。您必须在训练和测试集中省略目标变量。在
knn
调用中,将列车设置的目标变量传递给参数
cl
。那么它应该会起作用。 在本例中,目标变量位于第5列

cl
的长度不等于测试集中的行数时,会发生错误

library(class)
library(caret)

dat<-iris

ind <- createDataPartition(dat$Species, p=0.70, list=FALSE)
dat_train <- dat[ ind,-5]         #leave your target variable out 
dat_test <- dat[-ind,-5]          #leave your target variable out
cl<-dat[ind,5]                    #your target variable for the train set
knn(dat_train, dat_test, cl, k=2, prob=TRUE)
由于c1(6)的长度与csd_列车(57)的行数不匹配,因此无法工作

**另一编辑:

试试这个:

ind <- createDataPartition(CSD$Caesarian, p=0.70, list=FALSE)
csd_train <- CSD[ ind,-6]
csd_test <- CSD[-ind,-6]
c1 <- CSD[ ind,6]
knn(csd_train , csd_test, c1, k=2, prob=TRUE)

ind您能提供一个可再现的错误示例吗?您在第4行做什么(
>c1我希望c1只是最后一列-第4行包括列1-6,但不包括列1-5,只留下列6。@Alex您所说的“可再现示例”是什么意思?获取一些可供所有人使用的数据,并用这些数据重现您的错误。例如,我使用
iris
数据获得了您的错误。好的,嗯。因此我将尝试
c1如果您的目标变量位于
csd\u train
的第6列,那么它将是
c1我所做的
c1都是数值预测变量吗?它们是否包含ain缺少值?我认为以下是解决此问题的方法:
Error in knn(csd_train, csd_test, c1, k = 2, prob = TRUE) : NA/NaN/Inf in `foreign function call (arg 6) In addition: Warning messages: 1: In` `knn(csd_train, csd_test, c1, k = 2, prob = TRUE) : NAs introduced by coercion 2:` `In knn(csd_train, csd_test, c1, k = 2, prob = TRUE) : NAs introduced by coercion`
library(class)
library(caret)

dat<-iris

ind <- createDataPartition(dat$Species, p=0.70, list=FALSE)
dat_train <- dat[ ind,-5]         #leave your target variable out 
dat_test <- dat[-ind,-5]          #leave your target variable out
cl<-dat[ind,5]                    #your target variable for the train set
knn(dat_train, dat_test, cl, k=2, prob=TRUE)
> dim(csd_train)
 [1] 57  6
> dim(csd_test)
 [1] 23  6
> length(c1)
 [1] 6
ind <- createDataPartition(CSD$Caesarian, p=0.70, list=FALSE)
csd_train <- CSD[ ind,-6]
csd_test <- CSD[-ind,-6]
c1 <- CSD[ ind,6]
knn(csd_train , csd_test, c1, k=2, prob=TRUE)