Debian Codegraph @

R/xgb.create.features.R

raw
1 #' Create new features from a previously learned model
2 #'
3 #' May improve the learning by adding new features to the training data based on the
4 #' decision trees from a previously learned model.
5 #'
6 #' @details
7 #' This is the function inspired from the paragraph 3.1 of the paper:
8 #'
9 #' **Practical Lessons from Predicting Clicks on Ads at Facebook**
10 #'
11 #' *(Xinran He, Junfeng Pan, Ou Jin, Tianbing Xu, Bo Liu, Tao Xu, Yan, xin Shi, Antoine Atallah, Ralf Herbrich, Stuart Bowers,
12 #' Joaquin Quinonero Candela)*
13 #'
14 #' International Workshop on Data Mining for Online Advertising (ADKDD) - August 24, 2014
15 #'
16 #' \url{https://research.facebook.com/publications/practical-lessons-from-predicting-clicks-on-ads-at-facebook/}.
17 #'
18 #' Extract explaining the method:
19 #'
20 #' "We found that boosted decision trees are a powerful and very
21 #' convenient way to implement non-linear and tuple transformations
22 #' of the kind we just described. We treat each individual
23 #' tree as a categorical feature that takes as value the
24 #' index of the leaf an instance ends up falling in. We use
25 #' 1-of-K coding of this type of features.
26 #'
27 #' For example, consider the boosted tree model in Figure 1 with 2 subtrees,
28 #' where the first subtree has 3 leafs and the second 2 leafs. If an
29 #' instance ends up in leaf 2 in the first subtree and leaf 1 in
30 #' second subtree, the overall input to the linear classifier will
31 #' be the binary vector `[0, 1, 0, 1, 0]`, where the first 3 entries
32 #' correspond to the leaves of the first subtree and last 2 to
33 #' those of the second subtree.
34 #'
35 #' ...
36 #'
37 #' We can understand boosted decision tree
38 #' based transformation as a supervised feature encoding that
39 #' converts a real-valued vector into a compact binary-valued
40 #' vector. A traversal from root node to a leaf node represents
41 #' a rule on certain features."
42 #'
43 #' @param model Decision tree boosting model learned on the original data.
44 #' @param data Original data (usually provided as a `dgCMatrix` matrix).
45 #'
46 #' @return A `dgCMatrix` matrix including both the original data and the new features.
47 #'
48 #' @examples
49 #' data(agaricus.train, package = "xgboost")
50 #' data(agaricus.test, package = "xgboost")
51 #'
52 #' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
53 #' dtest <- with(agaricus.test, xgb.DMatrix(data, label = label, nthread = 2))
54 #'
55 #' param <- list(max_depth = 2, learning_rate = 1, objective = 'binary:logistic', nthread = 1)
56 #' nrounds = 4
57 #'
58 #' bst <- xgb.train(params = param, data = dtrain, nrounds = nrounds)
59 #'
60 #' # Model accuracy without new features
61 #' accuracy.before <- sum((predict(bst, agaricus.test$data) >= 0.5) == agaricus.test$label) /
62 #' length(agaricus.test$label)
63 #'
64 #' # Convert previous features to one hot encoding
65 #' new.features.train <- xgb.create.features(model = bst, agaricus.train$data)
66 #' new.features.test <- xgb.create.features(model = bst, agaricus.test$data)
67 #'
68 #' # learning with new features
69 #' new.dtrain <- xgb.DMatrix(
70 #' data = new.features.train, label = agaricus.train$label, nthread = 1
71 #' )
72 #' new.dtest <- xgb.DMatrix(
73 #' data = new.features.test, label = agaricus.test$label, nthread = 1
74 #' )
75 #' bst <- xgb.train(params = param, data = new.dtrain, nrounds = nrounds)
76 #'
77 #' # Model accuracy with new features
78 #' accuracy.after <- sum((predict(bst, new.dtest) >= 0.5) == agaricus.test$label) /
79 #' length(agaricus.test$label)
80 #'
81 #' # Here the accuracy was already good and is now perfect.
82 #' cat(paste("The accuracy was", accuracy.before, "before adding leaf features and it is now",
83 #' accuracy.after, "!\n"))
84 #'
85 #' @export
86 xgb.create.features <- function(model, data) {
87 pred_with_leaf <- predict.xgb.Booster(model, data, predleaf = TRUE)
88 cols <- lapply(as.data.frame(pred_with_leaf), factor)
89 cbind(data, sparse.model.matrix(~ . -1, cols)) # nolint
90 }