From the known conjugate of iris data, from the length and width of sepals and petals, I want to predict the species.
The data set is this:
I do not know how to get classes of the same size to get the neighboring knn. I tried:
data(iris)
normalize <- function(x){
return ((x-min(x))/max(x)-min(x))
}
iris_n <- as.data.frame(lapply(data[, c(1,2,3,4)],normalize))
iris_train <- c(iris_n[1:40, ], iris_n[51:90, ], iris_n[101:140, ])
iris_test <- c(iris_n[41:50, ], iris_n[91:100, ] ,iris_n[141:150, ])
iris_train_target <- c(iris_n[1:40, 5], iris_n[51:90, 5], iris_n[101:140, 5])
iris_test_target <-c(iris_n[41:50, 5], iris_n[91:100, 5], iris_n[141:150, 5])
require(class)
m1 <- knn(train = iris_train, test = iris_test, cl = iris_train_target, k=13)
table(iris_test_target, m1)
However the compiler tells me:
Loading required package: class
Error in knn(train = iris_train, test = iris_test, cl = iris_train_target, :
'train' and 'class' have different lengths
In addition: Warning message:
In is.na(cl) : is.na() applied to non-(list or vector) of type 'NULL'