在R:'火车'和'班级'有不同的长度

时间:2016-04-25 03:48:14

标签: r knn

我正在尝试在R中构建一个knn模型,我已经清理了我的数据并摆脱了异常值并将我的数值标准化。但是,当我尝试运行它时会出现:" knn中的错误(train = x_train,test = x_test,cl = x_train_target,k = 57):   '列车'和'班级'有不同的长度"。

非常感谢任何帮助或评论。谢谢!

#Eliminating NA's
nrow(d0[!complete.cases(d0),]) #which gave me 35 rows with NA's
d0 <- na.omit(d0)
nrow(d0[!complete.cases(d0),])

#Dealing with Outliiers
d0 <- subset(d0, Night.Mins > 0) 
d0 <-subset(d0, Night.Mins <350)


#Normalization

Churn <- d0$Churn.
d0$Area.Code <- NULL #Excluding 
d0$State     <- NULL
d0$Churn.    <- NULL
d0$Phone     <- NULL
d0$VMail.Plan <-NULL
d0$Int.l.Plan <- NULL
x_1 <- d0 

normalizer<- function(x) {
return( (x-min(x))/(max(x)-min(x)) ) }

x_1 <- as.data.frame(lapply(x[,c(1:15)], normalizer)) 
x_1$Churn. <- Churn

set.seed(9850)#mixer
y   <- runif(nrow(x))
x_1 <- x_1[order(y),]#mixed all of the data
trainrows <- sample (nrow(x_1),0.7*nrow(x_1))#training data
x_train   <- x_1[trainrows,]
x_test   <- x_1[-trainrows,]# test data
x_train_target <- x_1[trainrows,x_1$Churn.]
x_test_target <- x_1[-trainrows,x_1$Churn.]


require(class)
m1 <- knn( train= x_train, test= x_test, cl= x_train_target, k=57)}

0 个答案:

没有答案