@@ -62,7 +62,7 @@ def f(x):
62
62
all_models = {}
63
63
common_params = dict (
64
64
learning_rate = 0.05 ,
65
- n_estimators = 250 ,
65
+ n_estimators = 200 ,
66
66
max_depth = 2 ,
67
67
min_samples_leaf = 9 ,
68
68
min_samples_split = 9 ,
@@ -97,7 +97,7 @@ def f(x):
97
97
fig = plt .figure (figsize = (10 , 10 ))
98
98
plt .plot (xx , f (xx ), "g:" , linewidth = 3 , label = r"$f(x) = x\,\sin(x)$" )
99
99
plt .plot (X_test , y_test , "b." , markersize = 10 , label = "Test observations" )
100
- plt .plot (xx , y_med , "r-" , label = "Predicted median" , color = "orange" )
100
+ plt .plot (xx , y_med , "r-" , label = "Predicted median" )
101
101
plt .plot (xx , y_pred , "r-" , label = "Predicted mean" )
102
102
plt .plot (xx , y_upper , "k-" )
103
103
plt .plot (xx , y_lower , "k-" )
@@ -224,25 +224,24 @@ def coverage_fraction(y, y_low, y_high):
224
224
# underfit and could not adapt to sinusoidal shape of the signal.
225
225
#
226
226
# The hyper-parameters of the model were approximately hand-tuned for the
227
- # median regressor and there is no reason than the same hyper-parameters are
227
+ # median regressor and there is no reason that the same hyper-parameters are
228
228
# suitable for the 5th percentile regressor.
229
229
#
230
230
# To confirm this hypothesis, we tune the hyper-parameters of a new regressor
231
231
# of the 5th percentile by selecting the best model parameters by
232
232
# cross-validation on the pinball loss with alpha=0.05:
233
233
234
234
# %%
235
- from sklearn .model_selection import RandomizedSearchCV
235
+ from sklearn .experimental import enable_halving_search_cv # noqa
236
+ from sklearn .model_selection import HalvingRandomSearchCV
236
237
from sklearn .metrics import make_scorer
237
238
from pprint import pprint
238
239
239
-
240
240
param_grid = dict (
241
- learning_rate = [0.01 , 0.05 , 0.1 ],
242
- n_estimators = [100 , 150 , 200 , 250 , 300 ],
243
- max_depth = [2 , 5 , 10 , 15 , 20 ],
244
- min_samples_leaf = [1 , 5 , 10 , 20 , 30 , 50 ],
245
- min_samples_split = [2 , 5 , 10 , 20 , 30 , 50 ],
241
+ learning_rate = [0.05 , 0.1 , 0.2 ],
242
+ max_depth = [2 , 5 , 10 ],
243
+ min_samples_leaf = [1 , 5 , 10 , 20 ],
244
+ min_samples_split = [5 , 10 , 20 , 30 , 50 ],
246
245
)
247
246
alpha = 0.05
248
247
neg_mean_pinball_loss_05p_scorer = make_scorer (
@@ -251,20 +250,22 @@ def coverage_fraction(y, y_low, y_high):
251
250
greater_is_better = False , # maximize the negative loss
252
251
)
253
252
gbr = GradientBoostingRegressor (loss = "quantile" , alpha = alpha , random_state = 0 )
254
- search_05p = RandomizedSearchCV (
253
+ search_05p = HalvingRandomSearchCV (
255
254
gbr ,
256
255
param_grid ,
257
- n_iter = 10 , # increase this if computational budget allows
256
+ resource = "n_estimators" ,
257
+ max_resources = 250 ,
258
+ min_resources = 50 ,
258
259
scoring = neg_mean_pinball_loss_05p_scorer ,
259
260
n_jobs = 2 ,
260
261
random_state = 0 ,
261
262
).fit (X_train , y_train )
262
263
pprint (search_05p .best_params_ )
263
264
264
265
# %%
265
- # We observe that the search procedure identifies that deeper trees are needed
266
- # to get a good fit for the 5th percentile regressor. Deeper trees are more
267
- # expressive and less likely to underfit .
266
+ # We observe that the hyper-parameters that were hand-tuned for the median
267
+ # regressor are in the same range as the hyper-parameters suitable for the 5th
268
+ # percentile regressor .
268
269
#
269
270
# Let's now tune the hyper-parameters for the 95th percentile regressor. We
270
271
# need to redefine the `scoring` metric used to select the best model, along
@@ -286,15 +287,14 @@ def coverage_fraction(y, y_low, y_high):
286
287
pprint (search_95p .best_params_ )
287
288
288
289
# %%
289
- # This time, shallower trees are selected and lead to a more constant piecewise
290
- # and therefore more robust estimation of the 95th percentile. This is
291
- # beneficial as it avoids overfitting the large outliers of the log-norma
8000
l
292
- # additive noise.
293
- #
294
- # We can confirm this intuition by displaying the predicted 90% confidence
295
- # interval comprised by the predictions of those two tuned quantile regressors:
296
- # the prediction of the upper 95th percentile has a much coarser shape than the
297
- # prediction of the lower 5th percentile:
290
+ # The result shows that the hyper-parameters for the 95th percentile regressor
291
+ # identified by the search procedure are roughly in the same range as the hand-
292
+ # tuned hyper-parameters for the median regressor and the hyper-parameters
293
+ # identified by the search procedure for the 5th percentile regressor. However,
294
+ # the hyper-parameter searches did lead to an improved 90% confidence interval
295
+ # that is comprised by the predictions of those two tuned quantile regressors.
296
+ # Note that the prediction of the upper 95th percentile has a much coarser shape
297
+ # than the prediction of the lower 5th percentile because of the outliers:
298
298
y_lower = search_05p .predict (xx )
299
299
y_upper = search_95p .predict (xx )
300
300
0 commit comments