Created
March 3, 2020 09:37
-
-
Save myloginid/1e44d24a5e05160a1c565aba536c4e4a to your computer and use it in GitHub Desktop.
xgb-demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages(c("zoo","data.table","FeatureHashing","xgboost","Matrix","dplyr","smbinning","ROCR")) | |
install.packages(c("xgboost","DiagrammeR")) | |
data(agaricus.train, package='xgboost') | |
library(xgboost) | |
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 3, | |
eta = 1, nthread = 2, nrounds = 2,objective = "binary:logistic") | |
# plot all the trees | |
xgb.plot.tree(model = bst) | |
# plot only the first tree and display the node ID: | |
xgb.plot.tree(model = bst, trees = 0, show_node_id = TRUE) | |
print(bst) | |
## Not run: | |
# Below is an example of how to save this plot to a file. | |
# Note that for `export_graph` to work, the DiagrammeRsvg and rsvg packages must also be installed. | |
library(DiagrammeR) | |
gr <- xgb.plot.tree(model=bst, trees=0:1, render=FALSE) | |
export_graph(gr, 'tree.pdf', width=1500, height=1900) | |
export_graph(gr, 'tree.png', width=1500, height=1900) | |
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 15, | |
eta = 1, nthread = 2, nrounds = 30, objective = "binary:logistic", | |
min_child_weight = 50, verbose = 0) | |
p <- xgb.plot.multi.trees(model = bst, features_keep = 3) | |
print(p) | |
## Not run: | |
# Below is an example of how to save this plot to a file. | |
# Note that for `export_graph` to work, the DiagrammeRsvg and rsvg packages must also be installed. | |
library(DiagrammeR) | |
gr <- xgb.plot.multi.trees(model=bst, features_keep = 3, render=FALSE) | |
export_graph(gr, 'tree.pdf', width=1500, height=600) | |
## End(Not run) | |
data(agaricus.train) | |
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, max_depth = 3, | |
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic") | |
importance_matrix <- xgb.importance(colnames(agaricus.train$data), model = bst) | |
xgb.plot.importance(importance_matrix, rel_to_first = TRUE, xlab = "Relative importance") | |
(gg <- xgb.ggplot.importance(importance_matrix, measure = "Frequency", rel_to_first = TRUE)) | |
gg + ggplot2::ylab("Frequency") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment