-
Notifications
You must be signed in to change notification settings - Fork 106
new extract functions #518
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8397de8
new extract functions
topepo b965e25
missing hardhat remote
topepo 7cfa87f
extract_parsnip_spec -> extract_spec_parsnip
topepo 9720b71
notes about model extraction
topepo 0d60fcb
small text adjustments
topepo f262071
Merge branch 'master' into extracts
topepo 5ba9a48
Update R/extract.R
topepo 69253ef
extract test cases
topepo 2103c4c
less specific remote
topepo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #' Extract elements of a parsnip model object | ||
| #' | ||
| #' @description | ||
| #' These functions extract various elements from a parsnip object. If they do | ||
| #' not exist yet, an error is thrown. | ||
| #' | ||
| #' - `extract_spec_parsnip()` returns the parsnip model specification. | ||
| #' | ||
| #' - `extract_fit_engine()` returns the engine specific fit embedded within | ||
| #' a parsnip model fit. For example, when using [parsnip::linear_reg()] | ||
| #' with the `"lm"` engine, this returns the underlying `lm` object. | ||
| #' | ||
| #' @param x A parsnip `model_fit` object. | ||
| #' @param ... Not currently used. | ||
| #' @details | ||
| #' Extracting the underlying engine fit can be helpful for describing the | ||
| #' model (via `print()`, `summary()`, `plot()`, etc.) or for variable | ||
| #' importance/explainers. | ||
| #' | ||
| #' However, users should not invoke the `predict()` method on an extracted | ||
| #' model. There may be preprocessing operations that `parsnip` has executed on | ||
| #' the data prior to giving it to the model. Bypassing these can lead to errors | ||
| #' or silently generating incorrect predictions. | ||
| #' | ||
| #' **Good**: | ||
| #' ```r | ||
| #' parsnip_fit %>% predict(new_data) | ||
| #' ``` | ||
| #' | ||
| #' **Bad**: | ||
| #' ```r | ||
| #' parsnip_fit %>% extract_fit_engine() %>% predict(new_data) | ||
| #' ``` | ||
| #' @return | ||
| #' The extracted value from the parsnip object, `x`, as described in the description | ||
| #' section. | ||
| #' | ||
| #' @name extract-parsnip | ||
| #' @examples | ||
| #' lm_spec <- linear_reg() %>% set_engine("lm") | ||
| #' lm_fit <- fit(lm_spec, mpg ~ ., data = mtcars) | ||
| #' | ||
| #' lm_spec | ||
| #' extract_spec_parsnip(lm_fit) | ||
| #' | ||
| #' extract_fit_engine(lm_fit) | ||
| #' lm(mpg ~ ., data = mtcars) | ||
| NULL | ||
|
|
||
| #' @export | ||
| #' @rdname extract-parsnip | ||
| extract_spec_parsnip.model_fit <- function(x, ...) { | ||
| if (any(names(x) == "spec")) { | ||
| return(x$spec) | ||
| } | ||
| rlang::abort("Internal error: The model fit does not have a model spec.") | ||
| } | ||
|
|
||
|
|
||
| #' @export | ||
| #' @rdname extract-parsnip | ||
| extract_fit_engine.model_fit <- function(x, ...) { | ||
| if (any(names(x) == "fit")) { | ||
| return(x$fit) | ||
| } | ||
| rlang::abort("Internal error: The model fit does not have an engine fit.") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
|
|
||
| context("model extraction") | ||
|
|
||
| # ------------------------------------------------------------------------------ | ||
|
|
||
| test_that('extract', { | ||
| x <- linear_reg() %>% set_engine("lm") %>% fit(mpg ~ ., data = mtcars) | ||
| x_no_spec <- x | ||
| x_no_spec$spec <- NULL | ||
| x_no_fit <- x | ||
| x_no_fit$fit <- NULL | ||
|
|
||
| expect_true(inherits(extract_spec_parsnip(x), "model_spec")) | ||
| expect_true(inherits(extract_fit_engine(x), "lm")) | ||
|
|
||
| expect_error(extract_spec_parsnip(x_no_spec), "Internal error") | ||
| expect_error(extract_fit_engine(x_no_fit), "Internal error") | ||
| }) | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.