How to apply knn model on the test dataset after cross validation

I am trying to solve the well-known problem named Titanic- Machine Learning from Disaster.

I want to apply knn to predict the survived from the test dataset. I also want to use cross-validation and then want to apply it to my test dataset.

The code structure is given below:

install.packages("caret")
library(caret)

knn_2_train <- knn_1_train # train dataset
knn_2_train$Survived <- train$Survived
Survived <- as.factor(train$Survived) # train labels

knn_2_test <- knn_1_test # test dataset

trControl <- trainControl(method  = "cv", number  = 5)

fit <- train(knn_2_train, Survived,
             method = "knn",
             tuneGrid   = expand.grid(k = 1:50),
             metric     = "Accuracy",
             trControl  = trControl
             )

Now, I am not sure how can I apply the knn model for the test dataset after the cross-validation.

Any kind of suggestion is appreciable.

I have found the solution from the Stack Overflow post.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.