knn in R: 'train' and 'class' have different lengths -
i trying build knn model in r, have cleaned data , got rid off outlier , normalized numeri values. however, when try run comes : "error in knn(train = x_train, test = x_test, cl = x_train_target, k = 57) : 'train' , 'class' have different lengths".
any or comments appreciated. thank you!
#eliminating na's nrow(d0[!complete.cases(d0),]) #which gave me 35 rows na's d0 <- na.omit(d0) nrow(d0[!complete.cases(d0),]) #dealing 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 of 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)}
Comments
Post a Comment