-
Notifications
You must be signed in to change notification settings - Fork 3
/
compboost_class.R
103 lines (74 loc) · 2.7 KB
/
compboost_class.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ============================================================================ #
# compboost #
# ============================================================================ #
# We want to model the miles per gallon (mpg) dependent of the horse power
# (hp) as linear and quadratic learner and the weight (wt) just as linear
# learner.
# We want to use a 500 iterations as maxium, a lerning rate of 0.05 and
# quadratic loss.
# Prepare Data:
# -------------
df = mtcars
# Create new variable to check the polynomial baselearner with degree 2:
df$hp2 = df[["hp"]]^2
# Data for compboost:
X.hp = as.matrix(df[["hp"]], ncol = 1)
X.wt = as.matrix(df[["wt"]], ncol = 1)
y = df[["mpg"]]
# Hyperparameter for the algorithm:
learning.rate = 0.05
iter.max = 500
# Prepare compboost:
# ------------------
# Create new linear baselearner of hp and wt:
linear.factory.hp = PolynomialFactory$new(X.hp, "hp", 1)
linear.factory.wt = PolynomialFactory$new(X.wt, "wt", 1)
# Create new quadratic baselearner of hp:
quadratic.factory.hp = PolynomialFactory$new(X.hp, "hp", 2)
# Create new factory list:
factory.list = FactoryList$new()
# Register factorys:
factory.list$registerFactory(linear.factory.hp)
factory.list$registerFactory(linear.factory.wt)
factory.list$registerFactory(quadratic.factory.hp)
# Print the registered factorys:
factory.list$printRegisteredFactorys()
# Print model.frame:
factory.list$getModelFrame()
# Use quadratic loss:
loss.quadratic = QuadraticLoss$new()
# Use the greedy optimizer:
optimizer = GreedyOptimizer$new()
# Define logger. We want just the iterations as stopper but also track the
# time:
log.iterations = LogIterations$new(TRUE, iter.max)
log.time = LogTime$new(FALSE, 500, "microseconds")
logger.list = LoggerList$new()
logger.list$registerLogger(log.iterations)
logger.list$registerLogger(log.time)
logger.list$printRegisteredLogger()
# Run compboost:
# --------------
# Initialize object (Response, learning rate, stop if all stopper are fulfilled?,
# factory list, used loss, logger list):
cboost = Compboost$new(
response = y,
learning_rate = learning.rate,
stop_if_all_stopper_fulfilled = TRUE,
factory_list = factory.list,
loss = loss.quadratic,
logger_list = logger.list,
optimizer = optimizer
)
# Train the model (we want to print the trace):
cboost$train(trace = TRUE)
# Get vector selected baselearner:
cboost$getSelectedBaselearner()
# Get estimated parameter:
cboost$getEstimatedParameter()
# Get estimated parameter of 200 iteration:
cboost$getEstimatedParameterOfIteration(200)
# Get logger data:
cboost$getLoggerData()
# Get parameter matrix:
param.matrix = cboost$getParameterMatrix()