So I'm playing with the iris dataset and no matter what my value of k in the knn algorithm, I'm getting 100% accuracy. Have I gone wrong somewhere? Here's my code using the in-built iris data frame.
library(caret)
set.seed(52)
irissplit <- createDataPartition(iris$Species, p=.8, list=FALSE, times=1)
train <- iris[irissplit, ]
test <- iris[-irissplit, ]
library(class)
model_knn <- list()
accuracy_knn <- numeric()
for (i in 1:10) {
model_knn[[i]] <- knn(train[,-5], test[,-5], train$Species, k=i, prob=TRUE)
accuracy_knn[i] <- sum(model_knn[[i]]==test$Species)/length(test$Species)*100
}
accuracy_knn