| 1 |
## ----------------------------------------------------------------------------- |
| 2 |
library(xgboost) |
| 3 |
data(ToothGrowth) |
| 4 |
|
| 5 |
y <- ToothGrowth$supp # the response which we want to model/predict |
| 6 |
x <- ToothGrowth[, c("len", "dose")] # the features from which we want to predict it |
| 7 |
model <- xgboost(x, y, nthreads = 1, nrounds = 2) |
| 8 |
model |
| 9 |
|
| 10 |
## ----------------------------------------------------------------------------- |
| 11 |
predict(model, x[1:6, ], type = "response") # probabilities for y's last level ("VC") |
| 12 |
predict(model, x[1:6, ], type = "raw") # log-odds |
| 13 |
predict(model, x[1:6, ], type = "class") # class with highest probability |
| 14 |
|
| 15 |
## ----------------------------------------------------------------------------- |
| 16 |
data(mtcars) |
| 17 |
|
| 18 |
y <- mtcars$mpg |
| 19 |
x <- mtcars[, -1] |
| 20 |
model_gaussian <- xgboost(x, y, nthreads = 1, nrounds = 2) # default is squared loss (Gaussian) |
| 21 |
model_poisson <- xgboost(x, y, objective = "count:poisson", nthreads = 1, nrounds = 2) |
| 22 |
model_abserr <- xgboost(x, y, objective = "reg:absoluteerror", nthreads = 1, nrounds = 2) |
| 23 |
|
| 24 |
## ----------------------------------------------------------------------------- |
| 25 |
y <- ToothGrowth$supp |
| 26 |
x <- ToothGrowth[, c("len", "dose")] |
| 27 |
model_conservative <- xgboost( |
| 28 |
x, y, nthreads = 1, |
| 29 |
nrounds = 5, |
| 30 |
max_depth = 2, |
| 31 |
reg_lambda = 0.5, |
| 32 |
learning_rate = 0.15 |
| 33 |
) |
| 34 |
pred_conservative <- predict( |
| 35 |
model_conservative, |
| 36 |
x |
| 37 |
) |
| 38 |
pred_conservative[1:6] # probabilities are all closer to 0.5 now |
| 39 |
|
| 40 |
## ----------------------------------------------------------------------------- |
| 41 |
xgboost( |
| 42 |
x, y, nthreads = 1, |
| 43 |
eval_set = 0.2, |
| 44 |
monitor_training = TRUE, |
| 45 |
verbosity = 1, |
| 46 |
eval_metric = c("auc", "logloss"), |
| 47 |
nrounds = 5, |
| 48 |
max_depth = 2, |
| 49 |
reg_lambda = 0.5, |
| 50 |
learning_rate = 0.15 |
| 51 |
) |
| 52 |
|
| 53 |
## ----------------------------------------------------------------------------- |
| 54 |
attributes(model) |
| 55 |
|
| 56 |
## ----------------------------------------------------------------------------- |
| 57 |
xgb.attributes(model) |
| 58 |
|
| 59 |
## ----------------------------------------------------------------------------- |
| 60 |
xgb.importance(model) |
| 61 |
|
| 62 |
## ----------------------------------------------------------------------------- |
| 63 |
xgb.model.dt.tree(model) |
| 64 |
|
| 65 |
## ----------------------------------------------------------------------------- |
| 66 |
data("agaricus.train") |
| 67 |
dmatrix <- xgb.DMatrix( |
| 68 |
data = agaricus.train$data, # a sparse CSC matrix ('dgCMatrix') |
| 69 |
label = agaricus.train$label, # zeros and ones |
| 70 |
nthread = 1 |
| 71 |
) |
| 72 |
booster <- xgb.train( |
| 73 |
data = dmatrix, |
| 74 |
nrounds = 10, |
| 75 |
params = xgb.params( |
| 76 |
objective = "binary:logistic", |
| 77 |
nthread = 1, |
| 78 |
max_depth = 3 |
| 79 |
) |
| 80 |
) |
| 81 |
|
| 82 |
data("agaricus.test") |
| 83 |
dmatrix_test <- xgb.DMatrix(agaricus.test$data, nthread = 1) |
| 84 |
pred_prob <- predict(booster, dmatrix_test) |
| 85 |
pred_raw <- predict(booster, dmatrix_test, outputmargin = TRUE) |
| 86 |
|
| 87 |
## ----------------------------------------------------------------------------- |
| 88 |
xgb.importance(model) |
| 89 |
xgb.importance(booster) |
| 90 |
|