tidymodels / hardhat

Construct Modeling Packages
https://hardhat.tidymodels.org
Other
101 stars 16 forks source link

get_outcome_levels blueprint method? #206

Closed jonthegeek closed 2 years ago

jonthegeek commented 2 years ago

Feature

For "class" predict functions, we need to extract the levels from the blueprint. It took a little digging to figure out where those were. I thought get_levels or get_outcome_levels was pushing in that direction, but ended up just using levels(object$blueprint$ptypes$outcomes[[1]]). It feels like there should be a more obvious solution. Ideally the output should be a character vector if possible, since that's what we need for spruce_prob or to change numeric predictions to the proper labeled factor levels.

I may be missing an existing solution, but I can't find it if I am.

DavisVaughan commented 2 years ago

It is probably better to just store the levels in the fit object.

If you are following the model template then right here you have access to the outcomes, so just pluck off the levels and store then in the model result

https://github.com/tidymodels/hardhat/blob/13ad41a32de15ae4d67ed395696f7c738140fdd6/inst/templates/R/fit.R#L98-L108

DavisVaughan commented 2 years ago

get_outcome_levels() could be used on processed$outcomes if you want, but it takes into account the fact that you might have multiple outcome columns, which is why it returns a list of factor levels.

It is probably simpler for you to just call levels(outcome) there and store that.

I don't think modifying get_outcome_levels() is the right solution

jonthegeek commented 2 years ago

Oooh, storing them in the model object is a good option!

I definitely wasn't confident this was the right solution, but it wasn't clear what was. Perhaps a classification vignette would help?

FWIW, tabnet extracts them from the blueprint, which is what led me to that solution.

jonthegeek commented 2 years ago

I'll go ahead and close this issue, since this isn't the right solution. Thanks for the feedback!

github-actions[bot] commented 2 years ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.