2
$\begingroup$

This is my script in Rstudio:

library(class)
library(ggplot2)
library(gmodels)
library(scales)
library(caret)
library(tidyverse)
library(caret)

db_data <- iris
row_train <- sample(nrow(iris), nrow(iris)*0.8)
db_train <- iris[row_train,]
db_test <- iris[-row_train,]

unique(db_train$Species)
table(db_train$Species)
#--------

#KNN
#-------
model_knn<-train(Species ~ ., data = db_train, method = "knn",tuneGrid = data.frame(k = 12))
summary(model_knn)
#-------

#PREDICTION NEW RECORD
#-------
test_data <- db_test
db_test$predict <- predict(model_knn, newdata=test_data, interval='confidence')
confusionMatrix(data=factor(db_test$predict),reference=factor(db_test$Species))
#-------

How can I define the optimal value of k in the KNN model?

$\endgroup$

2 Answers 2

2
$\begingroup$

Usually you will use cross validation to find optimal model (hyper) parameter.

See this post for a application to KNN in R.

You may also have a look at the book "An Introduction to Statistical Learning" ch. 5 (Resampling Methods) to learn more about cross validation.

$\endgroup$
1
$\begingroup$

From what I have tried There is no effective way to know which K is optimal for your KNN model. You can try to set different values and see how your model behaves according to different values, maybe try to plot the error rate and K.

You can read a bit more about it here: https://towardsdatascience.com/how-to-find-the-optimal-value-of-k-in-knn-35d936e554eb

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.