Skip to content

Instantly share code, notes, and snippets.

@juliasilge
Created March 22, 2021 15:38
Show Gist options
  • Save juliasilge/26a43e5e68cf12842354e6652dfed688 to your computer and use it in GitHub Desktop.
Save juliasilge/26a43e5e68cf12842354e6652dfed688 to your computer and use it in GitHub Desktop.
Benchmark LiblineaR and glmnet engines for logistic regression in tidymodels
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