Skip to content

Make sure sparse matrices can be used with predict() #1167

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

Merged
merged 8 commits into from
Sep 5, 2024
Merged
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 @@

* `fit()` and `fit_xy()` can now take sparse tibbles as data values (#1165).

* `predict()` can now take dgCMatrix and sparse tibble input for `new_data` argument, and error informatively when model doesn't support it (#1167).

* Transitioned package errors and warnings to use cli (#1147 and #1148 by
@shum461, #1153 by @RobLBaker and @wright13, #1154 by @JamesHWade, #1160,
#1161, #1081).
Expand Down
4 changes: 4 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ check_xy_interface <- function(x, y, cl, model) {
}

allow_sparse <- function(x) {
if (inherits(x, "model_fit")) {
x <- x$spec
}
Comment on lines +447 to +449
Copy link
Member Author

Choose a reason for hiding this comment

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

allow_sparse() was assumed to only work on model_spec. This change makes it so it also works on model_fit objects

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for the additional context!


res <- get_from_env(paste0(class(x)[1], "_encoding"))
all(res$allow_sparse_x[res$engine == x$engine])
}
Expand Down
2 changes: 2 additions & 0 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ predict.model_fit <- function(object, new_data, type = NULL, opts = list(), ...)
}
check_pred_type_dots(object, type, ...)

new_data <- to_sparse_data_frame(new_data, object)

res <- switch(
type,
numeric = predict_numeric(object = object, new_data = new_data, ...),
Expand Down
8 changes: 8 additions & 0 deletions R/sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ to_sparse_data_frame <- function(x, object) {
if (allow_sparse(object)) {
x <- sparsevctrs::coerce_to_sparse_data_frame(x)
} else {
if (inherits(object, "model_fit")) {
object <- object$spec
}

cli::cli_abort(
"{.arg x} is a sparse matrix, but {.fn {class(object)[1]}} with
engine {.code {object$engine}} doesn't accept that.")
Expand All @@ -19,6 +23,10 @@ is_sparse_tibble <- function(x) {

materialize_sparse_tibble <- function(x, object, input) {
if (is_sparse_tibble(x) && (!allow_sparse(object))) {
if (inherits(object, "model_fit")) {
object <- object$spec
}

cli::cli_warn(
"{.arg {input}} is a sparse tibble, but {.fn {class(object)[1]}} with
engine {.code {object$engine}} doesn't accept that. Converting to
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/sparsevctrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
Error in `to_sparse_data_frame()`:
! `x` is a sparse matrix, but `linear_reg()` with engine `lm` doesn't accept that.

# sparse tibble can be passed to `predict()

Code
preds <- predict(lm_fit, sparse_mtcars)
Condition
Warning:
`x` is a sparse tibble, but `linear_reg()` with engine `lm` doesn't accept that. Converting to non-sparse.

# sparse matrices can be passed to `predict()

Code
predict(lm_fit, sparse_mtcars)
Condition
Error in `to_sparse_data_frame()`:
! `x` is a sparse matrix, but `linear_reg()` with engine `lm` doesn't accept that.

# to_sparse_data_frame() is used correctly

Code
Expand Down
60 changes: 60 additions & 0 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,66 @@ test_that("sparse matrices can be passed to `fit_xy()", {
)
})

test_that("sparse tibble can be passed to `predict()", {
skip_if_not_installed("ranger")

hotel_data <- sparse_hotel_rates()
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)

spec <- rand_forest(trees = 10) %>%
set_mode("regression") %>%
set_engine("ranger")

tree_fit <- fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1])

expect_no_error(
predict(tree_fit, hotel_data)
)

spec <- linear_reg() %>%
set_mode("regression") %>%
set_engine("lm")

lm_fit <- fit(spec, mpg ~ ., data = mtcars)

sparse_mtcars <- mtcars %>%
sparsevctrs::coerce_to_sparse_matrix() %>%
sparsevctrs::coerce_to_sparse_tibble()

expect_snapshot(
preds <- predict(lm_fit, sparse_mtcars)
)
})

test_that("sparse matrices can be passed to `predict()", {
skip_if_not_installed("ranger")

hotel_data <- sparse_hotel_rates()

spec <- rand_forest(trees = 10) %>%
set_mode("regression") %>%
set_engine("ranger")

tree_fit <- fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1])

expect_no_error(
predict(tree_fit, hotel_data)
)

spec <- linear_reg() %>%
set_mode("regression") %>%
set_engine("lm")

lm_fit <- fit(spec, mpg ~ ., data = mtcars)

sparse_mtcars <- sparsevctrs::coerce_to_sparse_matrix(mtcars)

expect_snapshot(
error = TRUE,
predict(lm_fit, sparse_mtcars)
)
})

test_that("to_sparse_data_frame() is used correctly", {
skip_if_not_installed("xgboost")

Expand Down
Loading