2

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
G5W
  • 36,531
  • 10
  • 47
  • 80
stixmcvix
  • 313
  • 1
  • 2
  • 10

0 Answers0