Problems with KNN using tidymodels

I am analyzing a database and I want to perform a KNN. I am using the 'tidymodels' library and when I run the model, I get the following error:

All models failed. See the `.notes` column.

# Tuning results
# 10-fold cross-validation repeated 5 times 

There were issues with some computations:

  - Error(s) x1000: Error in check_outcome(): ! For a classification model, the outcome should be a factor.

Use collect_notes(object) for more information.

The bbdd is composed of the following variables: -Age (numerical), sex (1 = Male, 0 = Female), cp (with values 1,2 and 3), trestbps (numerical), chol (1 or 0),fbs (1 or 0), restecg (1 or 0), thalach (numerical), thal (1 or 0), highbps (1 or 0) y target (1 or 0).

Attached is the code I have made:


heart_df - heart_df %% mutate(across(.cols = c(sex, chol,fbs, restecg,thal,highbps, target), .fns = as.factor))

head(heart_df)

set.seed(123)
heart_split - initial_split(heart_df)
heart_train - training(heart_split)
heart_test - testing(heart_split)

set.seed(234)
heart_folds - vfold_cv(heart_train, repeats = 5)

heart_rec - recipe(target ~., data = heart_train) 

knn_mod - nearest_neighbor(mode = classification, 
                 neighbors = tune(), 
                 weight_func = tune(), 
                 dist_power = tune()) %% 
  set_engine(kknn)

knn_wf - workflow() %% 
  add_recipe(heart_rec) %% 
  add_model(knn_mod)

knn_param - knn_wf %% 
  parameters()

knn_res - tune_grid(
  knn_wf,
  resamples = heart_folds,
  param_info = knn_param,
  metrics = metric_set(accuracy, roc_auc, sensitivity, specificity),
  control = control_grid(save_pred = TRUE, parallel_over = everything),
  grid = 20)

Topic k-nn machine-learning-model classification r machine-learning

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.