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

pass along nfeatures and feature_names to xgboost bundle #67

Merged

Conversation

EmilHvitfeldt
Copy link
Contributor

Some functions that are applied to xgboost models use the feature_names and/or nfeatures element of the model. This PR makes sure they aren't removed when bundling.

Should close #66 and deal with tidymodels/orbital#61

library(tidymodels)
library(bundle)
library(orbital)

mod <- boost_tree(trees = 5, mtry = 3) %>%
  set_mode("regression") %>%
  set_engine("xgboost") %>%
  fit(mpg ~ ., data = mtcars[1:25,])

mod$fit$feature_names
#>  [1] "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

vip::vi(mod)
#> # A tibble: 4 × 2
#>   Variable Importance
#>   <chr>         <dbl>
#> 1 hp           0.498 
#> 2 wt           0.213 
#> 3 disp         0.212 
#> 4 drat         0.0774

mod_bundle <- bundle(mod)
mod_new <- unbundle(mod_bundle)

mod_new$fit$feature_names
#>  [1] "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

vip::vi(mod_new)
#> # A tibble: 4 × 2
#>   Variable Importance
#>   <chr>         <dbl>
#> 1 hp           0.498 
#> 2 wt           0.213 
#> 3 disp         0.212 
#> 4 drat         0.0774

Created on 2024-11-07 with reprex v2.1.0

@juliasilge
Copy link
Member

Would you want to update the tests as well, to confirm these components are usable after bundling?

@EmilHvitfeldt
Copy link
Contributor Author

Done!

Copy link
Member

@juliasilge juliasilge left a comment

Choose a reason for hiding this comment

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

Thank you so much!

@juliasilge juliasilge merged commit 51fbfb4 into rstudio:main Nov 8, 2024
9 checks passed
@EmilHvitfeldt EmilHvitfeldt deleted the dont-remove-feature_names-xgboost branch November 8, 2024 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bundling XGBoost objects removes variable names when applying xgb.importance()
2 participants