Created
March 22, 2021 15:38
-
-
Save juliasilge/26a43e5e68cf12842354e6652dfed688 to your computer and use it in GitHub Desktop.
Benchmark LiblineaR and glmnet engines for logistic regression in tidymodels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidymodels) | |
library(textrecipes) | |
data("small_fine_foods") | |
sparse_bp <- hardhat::default_recipe_blueprint(composition = "dgCMatrix") | |
text_rec <- | |
recipe(score ~ review, data = training_data) %>% | |
step_tokenize(review) %>% | |
step_stopwords(review) %>% | |
step_tokenfilter(review, max_tokens = 1e3) %>% | |
step_tfidf(review) | |
liblinear_spec <- logistic_reg(penalty = 0.02, mixture = 1) %>% | |
set_engine("LiblineaR") %>% | |
set_mode("classification") | |
glmnet_spec <- logistic_reg(penalty = 0.02, mixture = 1) %>% | |
set_engine("glmnet") %>% | |
set_mode("classification") | |
liblinear <- | |
workflow() %>% | |
add_recipe(text_rec) %>% | |
add_model(liblinear_spec) | |
glmnet_sparse <- | |
workflow() %>% | |
add_recipe(text_rec, blueprint = sparse_bp) %>% | |
add_model(glmnet_spec) | |
glmnet_default <- | |
workflow() %>% | |
add_recipe(text_rec) %>% | |
add_model(glmnet_spec) | |
set.seed(123) | |
food_folds <- vfold_cv(training_data, v = 3) | |
library(bench) | |
results <- mark( | |
iterations = 10, check = FALSE, | |
liblinear = fit_resamples(liblinear, food_folds), | |
glmnet_sparse = fit_resamples(glmnet_sparse, food_folds), | |
glmnet_default = fit_resamples(glmnet_default, food_folds), | |
) | |
results | |
autoplot(results, type = "ridge") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment