Skip to content

Commit

Permalink
Merge pull request #1368 from tidymodels/sparse-matrix-prep
Browse files Browse the repository at this point in the history
make prep() work with sparse matrices
  • Loading branch information
EmilHvitfeldt authored Sep 10, 2024
2 parents 5fc1a5c + e2b3725 commit 1928571
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* `recipe()`, `prep()`, and `bake()` now work with sparse tibbles. (#1364, #1366)

* `recipe()` now works with sparse matrices. (#1364)
* `recipe()` and `prep()` now work with sparse matrices. (#1364, #1368)

# recipes 1.1.0

Expand Down
3 changes: 3 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ validate_training_data <- function(x, rec, fresh, call = rlang::caller_env()) {
}
x <- rec$template
} else {
if (is_sparse_matrix(x)) {
x <- sparsevctrs::coerce_to_sparse_tibble(x)
}
if (!is_tibble(x)) {
x <- as_tibble(x)
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,26 @@ test_that("recipe() accepts sparse matrices", {
)
})

test_that("prep() accepts sparse matrices", {
skip_if_not_installed("modeldata")

hotel_data <- sparse_hotel_rates()

rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data)

expect_no_error(
rec <- prep(rec_spec)
)

expect_true(
is_sparse_tibble(rec$template)
)

expect_no_error(
rec <- prep(rec_spec, training = hotel_data)
)

expect_true(
is_sparse_tibble(rec$template)
)
})

0 comments on commit 1928571

Please sign in to comment.