Skip to content

update should_use_sparsity() to work with lightgbm #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Increased the minimum required R version to R 4.1.

* Updated auto toggle sparsity to handle lightgbm engine (#290).

# workflows 1.2.0

## New features
Expand Down
55 changes: 34 additions & 21 deletions R/sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,48 @@
return("no")
}

log_fold <- -0.599333138645995 +
ifelse(sparsity < 0.836601307189543, 0.836601307189543 - sparsity, 0) *
-0.541581853008009 +
ifelse(n_rows < 16000, 16000 - n_rows, 0) * 3.23980908942813e-05 +
ifelse(n_rows > 16000, n_rows - 16000, 0) * -2.81001152147355e-06 +
ifelse(sparsity > 0.836601307189543, sparsity - 0.836601307189543, 0) *
9.82444255114058 +
ifelse(sparsity > 0.836601307189543, sparsity - 0.836601307189543, 0) *
ifelse(n_rows > 8000, n_rows - 8000, 0) *
7.27456967763306e-05 +
ifelse(sparsity > 0.836601307189543, sparsity - 0.836601307189543, 0) *
log_fold <- -0.104969175543923 +
(ifelse(sparsity < 0.864864864864865, 0.864864864864865 - sparsity, 0) *
-0.863987224202949) +
(ifelse(sparsity < 0.864864864864865, 0.864864864864865 - sparsity, 0) *
ifelse(n_rows < 16000, 16000 - n_rows, 0) *
7.24704434279721e-05) +
(ifelse(sparsity < 0.864864864864865, 0.864864864864865 - sparsity, 0) *
ifelse(n_rows > 16000, n_rows - 16000, 0) *
-9.34342073897278e-07) +
(ifelse(sparsity > 0.864864864864865, sparsity - 0.864864864864865, 0) *
9.28694536071564) +
(ifelse(sparsity > 0.864864864864865, sparsity - 0.864864864864865, 0) *
ifelse(n_rows < 8000, 8000 - n_rows, 0) *
-0.000798307404212627
-0.000439058585009898) +
(ifelse(sparsity > 0.864864864864865, sparsity - 0.864864864864865, 0) *
ifelse(n_rows > 8000, n_rows - 8000, 0) *
2.66853007695264e-05)

if (engine == "xgboost") {
log_fold <- log_fold +
ifelse(sparsity < 0.984615384615385, 0.984615384615385 - sparsity, 0) *
0.113098025073806 +
ifelse(n_rows < 8000, 8000 - n_rows, 0) * -9.77914237255269e-05 +
ifelse(n_rows > 8000, n_rows - 8000, 0) * 3.22657666511869e-06 +
ifelse(sparsity > 0.984615384615385, sparsity - 0.984615384615385, 0) *
41.5180348086939 +
0.913457808326756
(ifelse(sparsity < 0.984615384615385, 0.984615384615385 - sparsity, 0) *
-0.558117285856414) +
(ifelse(n_rows < 8000, 8000 - n_rows, 0) * -0.000104996462576298) +
(ifelse(n_rows > 8000, n_rows - 8000, 0) * 2.36552247695297e-06) +
(ifelse(sparsity > 0.984615384615385, sparsity - 0.984615384615385, 0) *
40.8016066125263) +
1.02539759976548

Check warning on line 93 in R/sparsevctrs.R

View check run for this annotation

Codecov / codecov/patch

R/sparsevctrs.R#L87-L93

Added lines #L87 - L93 were not covered by tests
}

if (engine == "LiblineaR") {
log_fold <- log_fold +
ifelse(sparsity > 0.836601307189543, sparsity - 0.836601307189543, 0) *
-5.39592564852111
(ifelse(sparsity < 0.864864864864865, 0.864864864864865 - sparsity, 0) *
-0.343818313721125) +
(ifelse(sparsity > 0.864864864864865, sparsity - 0.864864864864865, 0) *
-5.25579902213519) +
-0.204133402827292

Check warning on line 102 in R/sparsevctrs.R

View check run for this annotation

Codecov / codecov/patch

R/sparsevctrs.R#L98-L102

Added lines #L98 - L102 were not covered by tests
}

if (engine == "lightgbm") {
log_fold <- log_fold +
(ifelse(sparsity > 0.864864864864865, sparsity - 0.864864864864865, 0) *
-7.14043416548319)

Check warning on line 108 in R/sparsevctrs.R

View check run for this annotation

Codecov / codecov/patch

R/sparsevctrs.R#L106-L108

Added lines #L106 - L108 were not covered by tests
}

ifelse(log_fold > 0, "yes", "no")
Expand Down
6 changes: 4 additions & 2 deletions man/add_formula.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ test_that("toggle_sparsity changes auto to yes", {

ames <- ames[c(fcts, outcome)]
ames <- ames[1:100, ]
ames$MS_SubClass <- as.factor(seq_along(ames$MS_SubClass))

tree_spec <- parsnip::linear_reg("regression", "glmnet", penalty = 0)

Expand Down Expand Up @@ -380,9 +381,8 @@ test_that("toggle_sparsity changes auto to no", {

tree_spec <- parsnip::linear_reg("regression", "glmnet", penalty = 0)

# if we only dummy 1 variable it doesn't make the data sparse enough
rec_spec <- recipes::recipe(Sale_Price ~ ., data = ames) |>
recipes::step_dummy(MS_Zoning)
recipes::step_dummy(recipes::all_nominal_predictors())

wf_spec <- workflow(rec_spec, tree_spec)

Expand Down
Loading