|
19 | 19 | #' \code{\link{gbm.unify}} for \code{\link[gbm:gbm]{GBM models}} |
20 | 20 | #' |
21 | 21 | #' \code{\link{xgboost.unify}} for \code{\link[xgboost:xgboost]{XGBoost models}} |
22 | | -#' |
| 22 | +#' |
23 | 23 | #' \code{\link{lightgbm.unify}} for \code{\link[lightgbm:lightgbm]{LightGBM models}} |
24 | 24 | #' |
25 | 25 | #' \code{\link{ranger.unify}} for \code{\link[ranger:ranger]{ranger models}} |
|
40 | 40 | #' x <- gpboost::gpb.Dataset(sparse_data, label = as.matrix(data[,ncol(data)])) |
41 | 41 | #' gpb_data <- gpboost::gpb.Dataset.construct(x) |
42 | 42 | #' gpb_model <- gpboost::gpboost(data = gpb_data, params = param_gpb, |
43 | | -#' verbose = -1, num_threads = 0) |
| 43 | +#' verbose = -1, num_threads = 1) |
44 | 44 | #' unified_model <- gpboost.unify(gpb_model, sparse_data) |
45 | 45 | #' shaps <- treeshap(unified_model, data[1:2, ]) |
46 | 46 | #' plot_contribution(shaps, obs = 1) |
@@ -90,26 +90,26 @@ gpboost.unify <- function(gpb_model, data, recalculate = FALSE) { |
90 | 90 | df <- df[, c("tree_index", "split_index", "split_feature", "Decision.type", "threshold", "Yes", "No", "Missing", "split_gain", "internal_count")] |
91 | 91 | colnames(df) <- c("Tree", "Node", "Feature", "Decision.type", "Split", "Yes", "No", "Missing", "Prediction", "Cover") |
92 | 92 | attr(df, "sorted") <- NULL |
93 | | - |
| 93 | + |
94 | 94 | ID <- paste0(df$Node, "-", df$Tree) |
95 | 95 | df$Yes <- match(paste0(df$Yes, "-", df$Tree), ID) |
96 | 96 | df$No <- match(paste0(df$No, "-", df$Tree), ID) |
97 | 97 | df$Missing <- match(paste0(df$Missing, "-", df$Tree), ID) |
98 | | - |
| 98 | + |
99 | 99 | # Here we lose "Quality" information |
100 | 100 | df$Prediction[!is.na(df$Feature)] <- NA |
101 | | - |
| 101 | + |
102 | 102 | feature_names <- jsonlite::fromJSON(gpb_model$dump_model())$feature_names |
103 | 103 | data <- data[,colnames(data) %in% feature_names] |
104 | | - |
| 104 | + |
105 | 105 | ret <- list(model = as.data.frame(df), data = as.data.frame(data), feature_names = feature_names) |
106 | 106 | class(ret) <- "model_unified" |
107 | 107 | attr(ret, "missing_support") <- TRUE |
108 | 108 | attr(ret, "model") <- "gpboost" |
109 | | - |
| 109 | + |
110 | 110 | if (recalculate) { |
111 | 111 | ret <- set_reference_dataset(ret, as.data.frame(data)) |
112 | 112 | } |
113 | | - |
| 113 | + |
114 | 114 | return(ret) |
115 | 115 | } |
0 commit comments