How to create a function to create K values with train and test accuracy in R
Let us create an empty list which will contain the training and testing accuracy and the respective k values. The following code will generate the k values along with the training and testing accuracy.
The k values will be ranging from 3 to 200 with a step size of 2.
test_acc <- NULL
train_acc <- NULL
for (i in seq(3,200,2))
{
train_glass_pred <- knn(train=training_set,test=test_set,cl=training_set[,10],k=i)
train_acc <- c(train_acc,mean(train_glass_pred==training_set[,10]))
test_glass_pred <- knn(train = training_set, test = test_set, cl = training_set[,10], k=i)
test_acc <- c(test_acc,mean(test_glass_pred==test_set[,10]))
}