Skip to content

[Experimental] Convert xgboost to rpf objects - #55

Open
mnwright wants to merge 6 commits into
masterfrom
xgboost_to_rpf
Open

[Experimental] Convert xgboost to rpf objects#55
mnwright wants to merge 6 commits into
masterfrom
xgboost_to_rpf

Conversation

@mnwright

@mnwright mnwright commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator

Adds a function to convert xgboost to rpf objects. Not very fast, it seems the bottleneck is get_leaf_bounds(), which could be implemented in C++.

Example:

library(randomPlantedForest)
library(xgboost)
library(ggplot2)
library(patchwork)

# Fit xgboost
x <- as.matrix(mtcars[, -1])
y <- mtcars$mpg
xg <- xgboost(data = x[1:26, ], label = y[1:26],
              params = list(max_depth = 4, eta = .1),
              nrounds = 10, verbose = 0)

# Convert
xgrpf <- convert_xgboost_rpf(xg, x, y)

# Prediction seems to be fairly similar
max(abs(predict(xg, x) - predict(xgrpf, x)))

library(glex)
xg_g <- glex(xg, x = x)
rpf_g <- glex(xgrpf, x = as.data.frame(x))

# Intercept is different
xg_g$intercept - rpf_g$intercept

# Components mostly differ in intercept
autoplot(xg_g, "wt") + autoplot(rpf_g, "wt")
autoplot(xg_g, "cyl") + autoplot(rpf_g, "cyl")

@jemus42

jemus42 commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator

Wondering if glex should provide a purify S3 generic and a method for xgb, while this package can define the method for itself and just use it to call its internal method in C++.

Not that it matters much for the experimental part but I'd prefer to not have xgboost as an Imports here, maybe a Suggests but, well, might as well use glex for that functionality in any case.

@mnwright

mnwright commented Feb 7, 2024

Copy link
Copy Markdown
Collaborator Author

I agree. The C++ part (set_model) should stay here and the rest go to glex (or somewhere else). Just didn't want to have to two PRs.

@jemus42

jemus42 commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Forest serialization is now done in #63

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.

3 participants