Skip to content

Commit

Permalink
small speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Dec 17, 2024
1 parent 2d923ac commit 0c36b4c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions R/model-xgboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ xgboost_multisoft <- function(x, type, lvl) {
trees <- tidypredict::.extract_xgb_trees(x)

trees_split <- split(trees, rep(seq_along(lvl), x$niter))
trees_split <- lapply(trees_split, collapse_stumps)
trees_split <- vapply(trees_split, paste, character(1), collapse = " + ")
trees_split <- namespace_case_when(trees_split)

Expand All @@ -57,6 +58,19 @@ xgboost_multisoft <- function(x, type, lvl) {
res
}

collapse_stumps <- function(x) {
stump_ind <- lengths(x) == 2

stumps <- x[stump_ind]
trees <- x[!stump_ind]

stump_values <- lapply(stumps, function(x) eval(x[[2]][[3]]))
stump_values <- unlist(stump_values)
stump_values <- sum(stump_values)

c(stump_values, trees)
}

xgboost_logistic <- function(x, type, lvl) {
eq <- tidypredict::tidypredict_fit(x)

Expand Down

0 comments on commit 0c36b4c

Please sign in to comment.