-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtutorial.jl
237 lines (158 loc) · 4.54 KB
/
tutorial.jl
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using Pkg # hideall
Pkg.activate("_literate/EX-crabs-xgb/Project.toml")
Pkg.instantiate()
macro OUTPUT()
return isdefined(Main, :Franklin) ? Franklin.OUT_PATH[] : "/tmp/"
end;
# This example is inspired from [this
# post](https://www.analyticsvidhya.com/blog/2016/03/complete-guide-parameter-tuning-xgboost-with-codes-python/)
# showing how to use XGBoost.
#
# @@dropdown
# ## First steps
# @@
# @@dropdown-content
#
# MLJ provides a built-in function to load the Crabs dataset:
using MLJ
using StatsBase
using Random
using Plots
import DataFrames
import StableRNGs.StableRNG
Plots.scalefontsizes() #hide
Plots.scalefontsizes(1.1) #hide
X, y = @load_crabs # a table and a vector
X = DataFrames.DataFrame(X)
@show size(X)
@show y[1:3]
first(X, 3)
#-
schema(X)
# We are looking at a classification problem with the following classes:
levels(y)
# It's not a very big dataset so we will likely overfit it badly using something as
# sophisticated as XGBoost but it will do for a demonstration. Since our data set is
# ordered by target class, we'll be sure to create shuffled train/test index sets:
train, test = partition(collect(eachindex(y)), 0.70, rng=StableRNG(123))
XGBC = @load XGBoostClassifier
xgb_model = XGBC()
# Let's check whether the training and is balanced, `StatsBase.countmap` is useful for
# that:
countmap(y[train])
# which is pretty balanced. You could check the same on the test set and full set and the
# same comment would still hold.
#
#
#
# @@
# @@dropdown
# ## XGBoost machine
# @@
# @@dropdown-content
# Wrap a machine around an XGBoost model (XGB) and the data:
xgb = XGBC()
mach = machine(xgb, X, y)
# We will tune it varying the number of rounds used and generate a learning curve
r = range(xgb, :num_round, lower=50, upper=500)
curve = learning_curve(
mach,
range=r,
resolution=50,
measure=brier_loss,
)
# Let's have a look
plot(curve.parameter_values, curve.measurements)
xlabel!("Number of rounds", fontsize=14)
ylabel!("Brier loss", fontsize=14)
savefig(joinpath(@OUTPUT, "EX-crabs-xgb-curve1.svg")); # hide
# \figalt{Brier loss vs Num Round}{EX-crabs-xgb-curve1.svg}
#
# Not a lot of improvement after 300 rounds.
xgb.num_round = 300;
# @@dropdown
# ### More tuning (1)
# @@
# @@dropdown-content
# Let's now tune the maximum depth of each tree and the minimum child weight in the
# boosting.
r1 = range(xgb, :max_depth, lower=3, upper=10)
r2 = range(xgb, :min_child_weight, lower=0, upper=5)
tuned_model = TunedModel(
xgb,
tuning=Grid(resolution=8),
resampling=CV(rng=11),
ranges=[r1,r2],
measure=brier_loss,
)
mach = machine(tuned_model, X, y)
fit!(mach, rows=train)
# Let's visualize details about the tuning:
plot(mach)
savefig(joinpath(@OUTPUT, "EX-crabs-xgb-tuningplot.svg")); # hide
# \figalt{Hyperparameter tuningplot}{EX-crabs-xgb-tuningplot.svg}
# Let's extract the optimal model and inspect its parameters:
xgb = fitted_params(mach).best_model
@show xgb.max_depth
@show xgb.min_child_weight
#
#
# @@
# @@dropdown
# ### More tuning (2)
# @@
# @@dropdown-content
#
# Let's examine the effect of `gamma`. This time we'll use a visual approach:
mach = machine(xgb, X, y)
curve = learning_curve(
mach,
range= range(xgb, :gamma, lower=0, upper=10),
resolution=30,
measure=brier_loss,
);
plot(curve.parameter_values, curve.measurements)
xlabel!("gamma", fontsize=14)
ylabel!("Brier loss", fontsize=14)
savefig(joinpath(@OUTPUT, "EX-crabs-xgb-gamma.svg")); # hide
# \figalt{Tuning gamma}{EX-crabs-xgb-gamma.svg}
# The following choice looks about optimal:
xgb.gamma = 3.8
# performance.
#
# @@
# @@dropdown
# ### More tuning (3)
# @@
# @@dropdown-content
#
# Let's next examine the effect of `subsample` and `colsample_bytree`:
r1 = range(xgb, :subsample, lower=0.6, upper=1.0)
r2 = range(xgb, :colsample_bytree, lower=0.6, upper=1.0)
tuned_model = TunedModel(
xgb,
tuning=Grid(resolution=8),
resampling=CV(rng=234),
ranges=[r1,r2],
measure=brier_loss,
)
mach = machine(tuned_model, X, y)
fit!(mach, rows=train)
#-
plot(mach)
savefig(joinpath(@OUTPUT, "EX-crabs-xgb-tuningplot2.svg")); # hide
# \figalt{Hyperparameter tuningplot}{EX-crabs-xgb-tuningplot2.svg}
#
# Let's retrieve the best models:
xgb = fitted_params(mach).best_model
@show xgb.subsample
@show xgb.colsample_bytree
# We could continue with more fine tuning but given how small the dataset is, it doesn't
# make much sense. How does it fare on the test set?
ŷ = predict_mode(mach, rows=test)
round(accuracy(ŷ, y[test]), sigdigits=3)
# Not too bad.
#
# @@
#
# @@