PedroGonçalves
PedroGonçalves

Reputation: 3

train function in R caret package keeps crashing

I am training a kNN model with train function from the R caret package and a dataset of 2000 entries. I used the following code:

set.seed(400)
ctrl <- trainControl(method="none")
knnFit <- train(Class ~ ., data = ScaniaTrain, method = "knn", trControl = ctrl, tuneLength = 1)

but the R keeps crashing. How can I improve the performance of this function?

Upvotes: 0

Views: 413

Answers (1)

feebarscevicius
feebarscevicius

Reputation: 584

KNN is costly, it may not be possible to train a model if you have lots of columns in your data (or if you have lots of categorical variables that caret expands to dummy variables under the hood).

You can try to set the k parameter to a low value and see if it works:

knnFit <- train(
  Class ~ ., 
  data = ScaniaTrain, 
  method = "knn", 
  trControl = ctrl,
  tuneGrid = c(k=3)
)

Upvotes: 1

Related Questions