-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
boost_from_prediction.R
38 lines (32 loc) · 1.24 KB
/
boost_from_prediction.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
library(lightgbm)
# Load in the agaricus dataset
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
dtrain <- lgb.Dataset(agaricus.train$data, label = agaricus.train$label)
dtest <- lgb.Dataset.create.valid(dtrain, data = agaricus.test$data, label = agaricus.test$label)
valids <- list(eval = dtest, train = dtrain)
#--------------------Advanced features ---------------------------
# advanced: start from an initial base prediction
print("Start running example to start from an initial prediction")
# Train lightgbm for 1 round
param <- list(
num_leaves = 4L
, learning_rate = 1.0
, nthread = 2L
, objective = "binary"
)
bst <- lgb.train(param, dtrain, 1L, valids = valids)
# Note: we need the margin value instead of transformed prediction in set_init_score
ptrain <- predict(bst, agaricus.train$data, type = "raw")
ptest <- predict(bst, agaricus.test$data, type = "raw")
# set the init_score property of dtrain and dtest
# base margin is the base prediction we will boost from
set_field(dtrain, "init_score", ptrain)
set_field(dtest, "init_score", ptest)
print("This is result of boost from initial prediction")
bst <- lgb.train(
params = param
, data = dtrain
, nrounds = 5L
, valids = valids
)