dmlc / XGBoost.jl

XGBoost Julia Package
Other
289 stars 110 forks source link

Display Split conditions #162

Closed tylerjthomas9 closed 1 year ago

tylerjthomas9 commented 1 year ago

I modified the PR here using @ExpandingMan's suggested method for proper type inference. I am interested in having this feature, so I went ahead and created a PR with all the changes because the other one hasn't had activity recently.

Here is a MWE to replicate the results:

julia> using DataFrames

julia> using Random; Random.seed!(3);

julia> using XGBoost

julia> df = DataFrame(randn(10,3), ["kirk", "spock", "bones"])
10×3 DataFrame
 Row │ kirk       spock       bones      
     │ Float64    Float64     Float64    
─────┼───────────────────────────────────
   1 │  0.663934  -0.419345   -0.489801
   2 │  1.19064    0.420935   -0.321852
   3 │  0.713867   0.293724    0.0450463
   4 │ -1.3474    -0.402996    1.50831
   5 │ -0.458164   0.0399281  -0.83443
   6 │ -0.277555   0.149485    0.408656
   7 │ -1.79885   -1.1535      0.99213
   8 │ -0.177408  -0.818639    0.280188
   9 │ -1.26053   -1.60734     2.21421
  10 │  0.30378   -0.299256    0.384029

julia> bst = xgboost((df, randn(10)), num_round=10)
[ Info: XGBoost: starting training.
[ Info: [1]     train-rmse:0.57998637329114211
[ Info: [2]     train-rmse:0.48232409595403752
[ Info: [3]     train-rmse:0.40593080843433427
[ Info: [4]     train-rmse:0.34595769369793850
[ Info: [5]     train-rmse:0.29282108263987289
[ Info: [6]     train-rmse:0.24862819795032731
[ Info: [7]     train-rmse:0.21094418685218519
[ Info: [8]     train-rmse:0.17903024616536045
[ Info: [9]     train-rmse:0.15198720040980171
[ Info: [10]    train-rmse:0.12906074380448287
[ Info: Training rounds complete.
╭──── XGBoost.Booster ─────────────────────────────────────────────────────────────────╮
│  Features: ["kirk", "spock", "bones"]                                                │
│                                                                                      │
│          Parameter          Value                                                    │
│   ─────────────────────────────────                                                  │
│     validate_parameters     true                                                     │
│                                                                                      │
╰──── boosted rounds: 10 ──────────────────────────────────────────────────────────────╯

julia> ts = trees(bst)
10-element Vector{XGBoost.Node}:
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")
 XGBoost.Node(split_feature="bones")

julia> ts[1]
╭──── XGBoost.Node (id=0, depth=0) ────────────────────────────────────────────────────╮
│                                                                                      │
│     split_condition     yes     no     nmissing        gain        cover             │
│   ────────────────────────────────────────────────────────────────────────           │
│       0.396342576        1      2         1         1.86042714     10.0              │
│                                                                                      │
│   XGBoost Tree (from this node)                                                      │
│  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━                                                     │
│                │                                                                     │
│                ├── bones < 0.396                                                     │
│                │   ├── bones < 0.332: XGBoost.Node(leaf=-0.159539297)                │
│                │   └── bones ≥ 0.332: XGBoost.Node(leaf=-0.0306737479)               │
│                └── bones ≥ 0.396                                                     │
│                    ├── spock < -0.778                                                │
│                    │   ├── kirk < -1.53: XGBoost.Node(leaf=-0.0544514731)            │
│                    │   └── kirk ≥ -1.53: XGBoost.Node(leaf=0.00967349485)            │
│                    └── spock ≥ -0.778                                                │
│                        ├── kirk < -0.812: XGBoost.Node(leaf=0.0550933369)            │
│                        └── kirk ≥ -0.812: XGBoost.Node(leaf=0.228843644)             │
╰──── 2 children ──────────────────────────────────────────────────────────────────────╯
ExpandingMan commented 1 year ago

Looks good to me, thanks! Want to do the honors of bumping the Project.toml to 2.2.2?

tylerjthomas9 commented 1 year ago

Done!