Skip to content
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

add toggle_sparsity() #281

Merged
merged 13 commits into from
Jan 17, 2025
2 changes: 2 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ fit.workflow <- function(object, data, ..., calibration = NULL, control = contro
)
}

object <- toggle_sparsity(object, data)

workflow <- object
workflow <- .fit_pre(workflow, data)
workflow <- .fit_model(workflow, control)
Expand Down
76 changes: 76 additions & 0 deletions R/sparsevctrs.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,79 @@
is_sparse_matrix <- function(x) {
methods::is(x, "sparseMatrix")
}

toggle_sparsity <- function(object, data) {
toggle_sparse <- "no"
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved

if (allow_sparse(object$fit$actions$model$spec)) {
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
if ("recipe" %in% names(object$pre$actions)) {
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
est_sparsity <- recipes::.recipes_estimate_sparsity(
object$pre$actions$recipe$recipe
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
)
} else {
est_sparsity <- sparsevctrs::sparsity(data, 1000)
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
}

pred_log_fold <- pred_log_fold(
est_sparsity,
object$fit$actions$model$spec$engine,
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
nrow(data)
)
if (pred_log_fold > 0) {
toggle_sparse <- "yes"
}
}

object$pre$actions$recipe$recipe <- recipes::.recipes_toggle_sparse_args(
object$pre$actions$recipe$recipe,
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
choice = toggle_sparse
)
object
}

allow_sparse <- function(x) {
if (inherits(x, "model_fit")) {
x <- x$spec

Check warning on line 36 in R/sparsevctrs.R

View check run for this annotation

Codecov / codecov/patch

R/sparsevctrs.R#L36

Added line #L36 was not covered by tests
}
res <- parsnip::get_from_env(paste0(class(x)[1], "_encoding"))
all(res$allow_sparse_x[res$engine == x$engine])
}

pred_log_fold <- function(sparsity, model, n_rows) {
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
if (is.null(model) || model == "ranger") {
return("no")
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2025-01-15 at 1 32 52 PM

since ranger performs identically for both sparse and dense data, we turn off automatically


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) *
ifelse(n_rows < 8000, 8000 - n_rows, 0) *
-0.000798307404212627

if (model == "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

Check warning on line 69 in R/sparsevctrs.R

View check run for this annotation

Codecov / codecov/patch

R/sparsevctrs.R#L62-L69

Added lines #L62 - L69 were not covered by tests
}

if (model == "LiblineaR") {
log_fold <- log_fold +
ifelse(sparsity > 0.836601307189543, sparsity - 0.836601307189543, 0) *
-5.39592564852111

Check warning on line 75 in R/sparsevctrs.R

View check run for this annotation

Codecov / codecov/patch

R/sparsevctrs.R#L73-L75

Added lines #L73 - L75 were not covered by tests
}

log_fold
}
Loading