# binary classification using "gbtree":
data("ToothGrowth")
x <- ToothGrowth[, c("len", "dose")]
y <- ToothGrowth$supp
model_tree_binary <- xgboost(
x, y,
nrounds = 5L,
nthreads = 1L,
booster = "gbtree",
max_depth = 2L
)
xgb.importance(model_tree_binary)
# binary classification using "gblinear":
model_tree_linear <- xgboost(
x, y,
nrounds = 5L,
nthreads = 1L,
booster = "gblinear",
learning_rate = 0.3
)
xgb.importance(model_tree_linear)
# multi-class classification using "gbtree":
data("iris")
x <- iris[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
y <- iris$Species
model_tree_multi <- xgboost(
x, y,
nrounds = 5L,
nthreads = 1L,
booster = "gbtree",
max_depth = 3
)
# all classes clumped together:
xgb.importance(model_tree_multi)
# inspect importances separately for each class:
num_classes <- 3L
nrounds <- 5L
xgb.importance(
model_tree_multi, trees = seq(from = 1, by = num_classes, length.out = nrounds)
)
xgb.importance(
model_tree_multi, trees = seq(from = 2, by = num_classes, length.out = nrounds)
)
xgb.importance(
model_tree_multi, trees = seq(from = 3, by = num_classes, length.out = nrounds)
)
# multi-class classification using "gblinear":
model_linear_multi <- xgboost(
x, y,
nrounds = 5L,
nthreads = 1L,
booster = "gblinear",
learning_rate = 0.2
)
xgb.importance(model_linear_multi)
Run the code above in your browser using DataLab