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