/
test_boosting.py
636 lines (453 loc) · 25.3 KB
/
test_boosting.py
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
from os.path import dirname, join
import numpy
from numpy.testing import assert_array_almost_equal, assert_array_equal
import pandas
import pytest
from sklearn.metrics import mean_squared_error
from sksurv.ensemble import ComponentwiseGradientBoostingSurvivalAnalysis, GradientBoostingSurvivalAnalysis
from sksurv.testing import assert_cindex_almost_equal
from sksurv.util import Surv
CGBOOST_CUMHAZ_FILE = join(dirname(__file__), 'data', 'compnentwise-gradient-boosting-coxph-cumhazard.csv')
CGBOOST_SURV_FILE = join(dirname(__file__), 'data', 'compnentwise-gradient-boosting-coxph-surv.csv')
GBOOST_CUMHAZ_FILE = join(dirname(__file__), 'data', 'gradient-boosting-coxph-cumhazard.csv')
GBOOST_SURV_FILE = join(dirname(__file__), 'data', 'gradient-boosting-coxph-surv.csv')
def early_stopping_monitor(i, est, locals_):
"""Returns True on the 10th iteration. """
return i == 9
class TestGradientBoosting(object):
@staticmethod
def test_fit(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(n_estimators=100, max_depth=3, min_samples_split=10,
random_state=0)
model.fit(whas500_data.x, whas500_data.y)
assert model.max_features_ == 14
assert not hasattr(model, "oob_improvement_")
p = model.predict(whas500_data.x)
assert_cindex_almost_equal(whas500_data.y['fstat'], whas500_data.y['lenfol'], p,
(0.86272605091218779, 64826, 10309, 14, 14))
assert (100,) == model.train_score_.shape
with pytest.raises(ValueError, match="X has 2 features, but DecisionTreeRegressor is "
"expecting 14 features as input."):
model.predict(whas500_data.x[:, :2])
@staticmethod
def test_fit_subsample(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(n_estimators=50, max_features=8, subsample=0.6,
random_state=0)
model.fit(whas500_data.x, whas500_data.y)
assert model.max_features_ == 8
assert hasattr(model, "oob_improvement_")
incl_mask = numpy.ones(whas500_data.x.shape[0], dtype=bool)
incl_mask[[35, 111, 174, 206, 236, 268, 497]] = False
x_test = whas500_data.x[incl_mask]
y_test = whas500_data.y[incl_mask]
p = model.predict(x_test)
assert_cindex_almost_equal(y_test['fstat'], y_test['lenfol'], p,
(0.8330510326740247, 60985, 12221, 2, 14))
assert (50,) == model.train_score_.shape
assert (50,) == model.oob_improvement_.shape
with pytest.raises(ValueError, match="X has 2 features, but DecisionTreeRegressor is "
"expecting 14 features as input."):
model.predict(whas500_data.x[:, :2])
@staticmethod
@pytest.mark.slow
def test_fit_dropout(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(n_estimators=100, max_features=8,
learning_rate=1.0, dropout_rate=0.03,
random_state=0)
model.fit(whas500_data.x, whas500_data.y)
assert not hasattr(model, "oob_improvement_")
assert model.max_features_ == 8
p = model.predict(whas500_data.x)
assert_cindex_almost_equal(whas500_data.y['fstat'], whas500_data.y['lenfol'], p,
(0.9094333, 68343, 6806, 0, 14))
@staticmethod
def test_fit_int_param_as_float(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
# Account for https://github.com/scikit-learn/scikit-learn/pull/12344
max_depth = 4
model = GradientBoostingSurvivalAnalysis(
n_estimators=100.0,
max_depth=float(max_depth),
min_samples_split=10.0,
random_state=0)
params = model.get_params()
assert 100 == params["n_estimators"]
assert max_depth == params["max_depth"]
assert 10 == params["min_samples_split"]
model.set_params(max_leaf_nodes=15.0)
assert 15 == model.get_params()["max_leaf_nodes"]
model.fit(whas500_data.x, whas500_data.y)
p = model.predict(whas500_data.x)
assert_cindex_almost_equal(whas500_data.y['fstat'], whas500_data.y['lenfol'], p,
(0.90256690042449006, 67826, 7321, 2, 14))
@staticmethod
@pytest.mark.parametrize("fn,expected_file",
[("predict_survival_function", GBOOST_SURV_FILE),
("predict_cumulative_hazard_function", GBOOST_CUMHAZ_FILE)])
def test_predict_function(make_whas500, fn, expected_file):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(n_estimators=100, max_depth=2, random_state=0)
train_x, train_y = whas500_data.x[10:], whas500_data.y[10:]
model.fit(train_x, train_y)
test_x = whas500_data.x[:10]
surv_fn = getattr(model, fn)(test_x)
times = numpy.unique(train_y["lenfol"][train_y["fstat"]])
actual = numpy.row_stack([fn_gb(times) for fn_gb in surv_fn])
expected = numpy.loadtxt(expected_file, delimiter=",")
assert_array_almost_equal(actual, expected)
@staticmethod
def test_max_features(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(n_estimators=10, max_features="auto", max_depth=3, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
assert model.max_features_ == whas500_data.x.shape[1]
model.set_params(max_features="sqrt")
model.fit(whas500_data.x, whas500_data.y)
assert round(abs(model.max_features_ - int(numpy.sqrt(whas500_data.x.shape[1]))), 7) == 0
model.set_params(max_features="log2")
model.fit(whas500_data.x, whas500_data.y)
assert round(abs(model.max_features_ - int(numpy.log2(whas500_data.x.shape[1]))), 7) == 0
model.set_params(max_features=0.25)
model.fit(whas500_data.x, whas500_data.y)
assert round(abs(model.max_features_ - int(0.25 * whas500_data.x.shape[1])), 7) == 0
model.set_params(max_features=5)
model.fit(whas500_data.x, whas500_data.y)
assert round(abs(model.max_features_ - 5), 7) == 0
model.set_params(max_features=-1)
with pytest.raises(ValueError,
match=r"max_features must be in \(0, n_features\]"):
model.fit(whas500_data.x, whas500_data.y)
model.set_params(max_features=-1.125)
with pytest.raises(ValueError,
match=r"max_features must be in \(0, 1.0\]"):
model.fit(whas500_data.x, whas500_data.y)
model.set_params(max_features="fail_me")
with pytest.raises(ValueError,
match="Invalid value for max_features: 'fail_me'. "
"Allowed string values are 'auto', 'sqrt' "
"or 'log2'"):
model.fit(whas500_data.x, whas500_data.y)
@staticmethod
def test_ccp_alpha(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
est_full = GradientBoostingSurvivalAnalysis(
n_estimators=10,
max_leaf_nodes=20,
random_state=1)
est_full.fit(whas500_data.x, whas500_data.y)
est_pruned = GradientBoostingSurvivalAnalysis(
n_estimators=10,
max_leaf_nodes=20,
ccp_alpha=10.0,
random_state=1)
est_pruned.fit(whas500_data.x, whas500_data.y)
tree = est_full.estimators_[0, 0].tree_
subtree = est_pruned.estimators_[0, 0].tree_
assert tree.node_count > subtree.node_count
assert tree.max_depth > subtree.max_depth
@staticmethod
def test_negative_ccp_alpha(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
clf = GradientBoostingSurvivalAnalysis()
msg = "ccp_alpha must be greater than or equal to 0"
with pytest.raises(ValueError, match=msg):
clf.set_params(ccp_alpha=-1.0)
clf.fit(whas500_data.x, whas500_data.y)
@staticmethod
def test_fit_verbose(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(n_estimators=10, verbose=1, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
@staticmethod
def test_ipcwls_loss(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(loss="ipcwls", n_estimators=100, max_depth=3, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
time_predicted = model.predict(whas500_data.x)
time_true = whas500_data.y["lenfol"]
event_true = whas500_data.y["fstat"]
rmse_all = numpy.sqrt(mean_squared_error(time_true, time_predicted))
assert round(abs(rmse_all - 590.5441693629117), 7) == 0
rmse_uncensored = numpy.sqrt(mean_squared_error(time_true[event_true], time_predicted[event_true]))
assert round(abs(rmse_uncensored - 392.97741487479743), 7) == 0
cindex = model.score(whas500_data.x, whas500_data.y)
assert round(abs(cindex - 0.8979161399), 7) == 0
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_survival_function(whas500_data.x)
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_cumulative_hazard_function(whas500_data.x)
@staticmethod
def test_squared_loss(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = GradientBoostingSurvivalAnalysis(loss="squared", n_estimators=100, max_depth=3, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
time_predicted = model.predict(whas500_data.x)
time_true = whas500_data.y["lenfol"]
event_true = whas500_data.y["fstat"]
rmse_all = numpy.sqrt(mean_squared_error(time_true, time_predicted))
assert round(abs(rmse_all - 580.23345259002951), 7) == 0
rmse_uncensored = numpy.sqrt(mean_squared_error(time_true[event_true], time_predicted[event_true]))
assert round(abs(rmse_uncensored - 383.10639243317951), 7) == 0
cindex = model.score(whas500_data.x, whas500_data.y)
assert round(abs(cindex - 0.9021810004), 7) == 0
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_survival_function(whas500_data.x)
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_cumulative_hazard_function(whas500_data.x)
@staticmethod
def test_ipcw_loss_staged_predict(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
# Test whether staged decision function eventually gives
# the same prediction.
model = GradientBoostingSurvivalAnalysis(loss="ipcwls", n_estimators=100, max_depth=3, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
y_pred = model.predict(whas500_data.x)
# test if prediction for last stage equals ``predict``
for y in model.staged_predict(whas500_data.x):
assert y.shape == y_pred.shape
assert_array_equal(y_pred, y)
model.set_params(dropout_rate=0.03)
model.fit(whas500_data.x, whas500_data.y)
y_pred = model.predict(whas500_data.x)
# test if prediction for last stage equals ``predict``
for y in model.staged_predict(whas500_data.x):
assert y.shape == y_pred.shape
assert_array_equal(y_pred, y)
@staticmethod
def test_squared_loss_staged_predict(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
# Test whether staged decision function eventually gives
# the same prediction.
model = GradientBoostingSurvivalAnalysis(loss="squared", n_estimators=100, max_depth=3, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
y_pred = model.predict(whas500_data.x)
# test if prediction for last stage equals ``predict``
for y in model.staged_predict(whas500_data.x):
assert y.shape == y_pred.shape
assert_array_equal(y_pred, y)
model.set_params(dropout_rate=0.03)
model.fit(whas500_data.x, whas500_data.y)
y_pred = model.predict(whas500_data.x)
# test if prediction for last stage equals ``predict``
for y in model.staged_predict(whas500_data.x):
assert y.shape == y_pred.shape
assert_array_equal(y_pred, y)
@staticmethod
def test_monitor_early_stopping(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
est = GradientBoostingSurvivalAnalysis(loss="ipcwls", n_estimators=50, max_depth=1,
subsample=0.5,
random_state=0)
est.fit(whas500_data.x, whas500_data.y, monitor=early_stopping_monitor)
assert est.n_estimators == 50 # this is not altered
assert est.estimators_.shape[0] == 10
assert est.train_score_.shape[0] == 10
assert est.oob_improvement_.shape[0] == 10
class TestSparseGradientBoosting(object):
@staticmethod
@pytest.mark.parametrize('loss', ['coxph', 'squared', 'ipcwls'])
def test_fit(whas500_sparse_data, loss):
model = GradientBoostingSurvivalAnalysis(loss=loss, n_estimators=100, max_depth=1, min_samples_split=10,
subsample=0.5, random_state=0)
model.fit(whas500_sparse_data.x_sparse, whas500_sparse_data.y)
assert model.estimators_.shape[0] == 100
assert model.train_score_.shape == (100,)
assert model.oob_improvement_.shape == (100,)
sparse_predict = model.predict(whas500_sparse_data.x_sparse)
model.fit(whas500_sparse_data.x_sparse, whas500_sparse_data.y)
dense_predict = model.predict(whas500_sparse_data.x_dense)
assert_array_almost_equal(sparse_predict, dense_predict)
@staticmethod
@pytest.mark.parametrize('loss', ['coxph', 'squared', 'ipcwls'])
@pytest.mark.slow
def test_dropout(whas500_sparse_data, loss):
model = GradientBoostingSurvivalAnalysis(loss=loss, n_estimators=100, max_depth=1, min_samples_split=10,
dropout_rate=0.03, random_state=0)
model.fit(whas500_sparse_data.x_sparse, whas500_sparse_data.y)
assert model.estimators_.shape[0] == 100
assert model.train_score_.shape == (100,)
sparse_predict = model.predict(whas500_sparse_data.x_sparse)
model.fit(whas500_sparse_data.x_dense, whas500_sparse_data.y)
dense_predict = model.predict(whas500_sparse_data.x_dense)
assert_array_almost_equal(sparse_predict, dense_predict)
class TestComponentwiseGradientBoosting(object):
@staticmethod
def test_fit(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100)
model.fit(whas500_data.x, whas500_data.y)
p = model.predict(whas500_data.x)
assert_cindex_almost_equal(whas500_data.y['fstat'], whas500_data.y['lenfol'], p,
(0.7755659, 58283, 16866, 0, 14))
expected_coef = pandas.Series(numpy.zeros(15, dtype=float), index=whas500_data.names)
expected_coef['age'] = 0.040919
expected_coef['hr'] = 0.004977
expected_coef['diasbp'] = -0.003407
expected_coef['bmi'] = -0.017938
expected_coef['sho'] = 0.429904
expected_coef['chf'] = 0.508211
assert_array_almost_equal(expected_coef.values, model.coef_)
assert (100,) == model.train_score_.shape
with pytest.raises(ValueError, match="X has 2 features, but ComponentwiseGradientBoostingSurvivalAnalysis is "
"expecting 14 features as input."):
model.predict(whas500_data.x[:, :2])
@staticmethod
def test_fit_subsample(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100, subsample=0.6, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
p = model.predict(whas500_data.x)
assert_cindex_almost_equal(whas500_data.y['fstat'], whas500_data.y['lenfol'], p,
(0.7750602, 58245, 16904, 0, 14))
expected_coef = pandas.Series(numpy.zeros(15, dtype=float), index=whas500_data.names)
expected_coef['age'] = 0.041299
expected_coef['hr'] = 0.00487
expected_coef['diasbp'] = -0.003381
expected_coef['bmi'] = -0.017018
expected_coef['sho'] = 0.433685
expected_coef['chf'] = 0.510277
assert_array_almost_equal(expected_coef.values, model.coef_)
assert (100,) == model.train_score_.shape
assert (100,) == model.oob_improvement_.shape
with pytest.raises(ValueError, match="X has 2 features, but ComponentwiseGradientBoostingSurvivalAnalysis is "
"expecting 14 features as input."):
model.predict(whas500_data.x[:, :2])
@staticmethod
def test_fit_dropout(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100, learning_rate=1.0,
dropout_rate=0.03, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
p = model.predict(whas500_data.x)
assert_cindex_almost_equal(whas500_data.y['fstat'], whas500_data.y['lenfol'], p,
(0.7772425, 58409, 16740, 0, 14))
expected_coef = pandas.Series(numpy.zeros(15, dtype=float), index=whas500_data.names)
expected_coef['age'] = 0.275537
expected_coef['hr'] = 0.040048
expected_coef['diasbp'] = -0.029998
expected_coef['bmi'] = -0.138909
expected_coef['sho'] = 3.318941
expected_coef['chf'] = 2.851386
expected_coef['mitype'] = -0.075817
assert_array_almost_equal(expected_coef.values, model.coef_)
@staticmethod
@pytest.mark.parametrize("fn,expected_file",
[("predict_survival_function", CGBOOST_SURV_FILE),
("predict_cumulative_hazard_function", CGBOOST_CUMHAZ_FILE)])
def test_predict_function(make_whas500, fn, expected_file):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100, random_state=0)
train_x, train_y = whas500_data.x[10:], whas500_data.y[10:]
model.fit(train_x, train_y)
test_x = whas500_data.x[:10]
surv_fn = getattr(model, fn)(test_x)
times = numpy.unique(train_y["lenfol"][train_y["fstat"]])
actual = numpy.row_stack([fn_gb(times) for fn_gb in surv_fn])
expected = numpy.loadtxt(expected_file, delimiter=",")
assert_array_almost_equal(actual, expected)
@staticmethod
def test_feature_importances(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
assert whas500_data.x.shape[1] + 1 == len(model.feature_importances_)
@staticmethod
def test_fit_verbose(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=10, verbose=1, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
@staticmethod
def test_ipcwls_loss(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(loss="ipcwls", n_estimators=100, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
time_predicted = model.predict(whas500_data.x)
time_true = whas500_data.y["lenfol"]
event_true = whas500_data.y["fstat"]
rmse_all = numpy.sqrt(mean_squared_error(time_true, time_predicted))
assert round(abs(rmse_all - 806.283308322), 7) == 0
rmse_uncensored = numpy.sqrt(mean_squared_error(time_true[event_true], time_predicted[event_true]))
assert round(abs(rmse_uncensored - 542.884585289), 7) == 0
cindex = model.score(whas500_data.x, whas500_data.y)
assert round(abs(cindex - 0.7773356931), 7) == 0
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_survival_function(whas500_data.x)
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_cumulative_hazard_function(whas500_data.x)
@staticmethod
def test_squared_loss(make_whas500):
whas500_data = make_whas500(with_std=False, to_numeric=True)
model = ComponentwiseGradientBoostingSurvivalAnalysis(loss="squared", n_estimators=100, random_state=0)
model.fit(whas500_data.x, whas500_data.y)
time_predicted = model.predict(whas500_data.x)
time_true = whas500_data.y["lenfol"]
event_true = whas500_data.y["fstat"]
rmse_all = numpy.sqrt(mean_squared_error(time_true, time_predicted))
assert round(abs(rmse_all - 793.6256945839657), 7) == 0
rmse_uncensored = numpy.sqrt(mean_squared_error(time_true[event_true], time_predicted[event_true]))
assert round(abs(rmse_uncensored - 542.83358120153525), 7) == 0
cindex = model.score(whas500_data.x, whas500_data.y)
assert round(abs(cindex - 0.7777082862), 7) == 0
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_survival_function(whas500_data.x)
with pytest.raises(ValueError, match="`fit` must be called with the loss option set to 'coxph'"):
model.predict_cumulative_hazard_function(whas500_data.x)
@pytest.fixture(params=[GradientBoostingSurvivalAnalysis, ComponentwiseGradientBoostingSurvivalAnalysis])
def sample_gb_class(request):
x = numpy.arange(100).reshape(5, 20)
y = Surv.from_arrays([False, False, True, True, False], [12, 14, 6, 9, 1])
return request.param, x, y
def test_param_n_estimators(sample_gb_class):
est_cls, x, y = sample_gb_class
model = est_cls(n_estimators=0)
with pytest.raises(ValueError, match="n_estimators must be greater than 0 but was 0"):
model.fit(x, y)
model.set_params(n_estimators=-1)
with pytest.raises(ValueError, match="n_estimators must be greater than 0 but was -1"):
model.fit(x, y)
def test_param_learning_rate(sample_gb_class):
est_cls, x, y = sample_gb_class
model = est_cls(learning_rate=0)
with pytest.raises(ValueError, match="learning_rate must be within ]0; 1] but was 0"):
model.fit(x, y)
model.set_params(learning_rate=1.2)
with pytest.raises(ValueError, match="learning_rate must be within ]0; 1] but was 1.2"):
model.fit(x, y)
def test_param_subsample(sample_gb_class):
est_cls, x, y = sample_gb_class
model = est_cls(subsample=0)
with pytest.raises(ValueError, match="subsample must be in ]0; 1] but was 0"):
model.fit(x, y)
model.set_params(subsample=1.2)
with pytest.raises(ValueError, match="subsample must be in ]0; 1] but was 1.2"):
model.fit(x, y)
def test_param_dropout_rate(sample_gb_class):
est_cls, x, y = sample_gb_class
model = est_cls(dropout_rate=-0.1)
with pytest.raises(ValueError, match=r"dropout_rate must be within \[0; 1\[, but was -0.1"):
model.fit(x, y)
model.set_params(dropout_rate=1.2)
with pytest.raises(ValueError, match=r"dropout_rate must be within \[0; 1\[, but was 1.2"):
model.fit(x, y)
def test_param_sample_weight(sample_gb_class):
est_cls, x, y = sample_gb_class
model = est_cls()
with pytest.raises(ValueError, match=r"Found input variables with inconsistent numbers of samples: \[5, 3\]"):
model.fit(x, y, [2, 3, 4])
model.set_params(dropout_rate=1.2)
with pytest.raises(ValueError, match=r"Found input variables with inconsistent numbers of samples: \[5, 8\]"):
model.fit(x, y, [2, 4, 5, 6, 7, 1, 2, 7])
def test_param_loss(sample_gb_class):
est_cls, x, y = sample_gb_class
model = est_cls(loss="")
with pytest.raises(ValueError, match="Loss '' not supported"):
model.fit(x, y)
model.set_params(loss="unknown")
with pytest.raises(ValueError, match="Loss 'unknown' not supported"):
model.fit(x, y)
model.set_params(loss=None)
with pytest.raises(ValueError, match="Loss None not supported"):
model.fit(x, y)