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, 119)) 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='Dimensions of X are inconsistent with training data: ' 'expected 14 features, but got 2'): model.predict(whas500_data.x[:, :2])
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_)
def test_fit_subsample(self): model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100, subsample=0.6, random_state=0) model.fit(self.x, self.y) p = model.predict(self.x) result = concordance_index_censored(self.y['fstat'], self.y['lenfol'], p) expected_cindex = numpy.array([0.7750602, 58245, 16904, 0, 119]) assert_array_almost_equal(expected_cindex, numpy.array(result)) coef_index = ['(Intercept)'] + self.columns expected_coef = pandas.Series(numpy.zeros(15, dtype=float), index=coef_index) 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_) self.assertTupleEqual((100,), model.train_score_.shape) self.assertTupleEqual((100,), model.oob_improvement_.shape) self.assertRaisesRegex(ValueError, 'Dimensions of X are inconsistent with training data: ' 'expected 14 features, but got 2', model.predict, self.x[:, :2])
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, 119)) 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='Dimensions of X are inconsistent with training data: ' 'expected 14 features, but got 2'): model.predict(whas500_data.x[:, :2])
def test_fit_dropout(self): model = ComponentwiseGradientBoostingSurvivalAnalysis( n_estimators=100, learning_rate=1.0, dropout_rate=0.03, random_state=0) model.fit(self.x, self.y) p = model.predict(self.x) result = concordance_index_censored(self.y['fstat'], self.y['lenfol'], p) expected_cindex = numpy.array([0.7772425, 58409, 16740, 0, 119]) assert_array_almost_equal(expected_cindex, numpy.array(result)) coef_index = ['(Intercept)'] + self.columns expected_coef = pandas.Series(numpy.zeros(15, dtype=float), index=coef_index) 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_)
def test_fit(self): model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100) model.fit(self.x, self.y) p = model.predict(self.x) result = concordance_index_censored(self.y['fstat'], self.y['lenfol'], p) expected_cindex = numpy.array([0.7755659, 58283, 16866, 0, 119]) assert_array_almost_equal(expected_cindex, numpy.array(result)) coef_index = ['(Intercept)'] + self.columns expected_coef = pandas.Series(numpy.zeros(15, dtype=float), index=coef_index) 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_) self.assertTupleEqual((100,), model.train_score_.shape) self.assertRaisesRegex(ValueError, 'Dimensions of X are inconsistent with training data: ' 'expected 14 features, but got 2', model.predict, self.x[:, :2])
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)
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_)
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)
def test_squared_loss(self): model = ComponentwiseGradientBoostingSurvivalAnalysis(loss="squared", n_estimators=100, random_state=0) model.fit(self.x, self.y) time_predicted = model.predict(self.x) time_true = self.y["lenfol"] event_true = self.y["fstat"] rmse_all = numpy.sqrt(mean_squared_error(time_true, time_predicted)) self.assertAlmostEqual(rmse_all, 793.6256945839657) rmse_uncensored = numpy.sqrt(mean_squared_error(time_true[event_true], time_predicted[event_true])) self.assertAlmostEqual(rmse_uncensored, 542.83358120153525)
def test_ipcwls_loss(self): model = ComponentwiseGradientBoostingSurvivalAnalysis(loss="ipcwls", n_estimators=100, random_state=0) model.fit(self.x, self.y) time_predicted = model.predict(self.x) time_true = self.y["lenfol"] event_true = self.y["fstat"] rmse_all = numpy.sqrt(mean_squared_error(time_true, time_predicted)) self.assertAlmostEqual(rmse_all, 806.283308322) rmse_uncensored = numpy.sqrt(mean_squared_error(time_true[event_true], time_predicted[event_true])) self.assertAlmostEqual(rmse_uncensored, 542.884585289)
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
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)
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
def test_fit_verbose(self): model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=10, verbose=1, random_state=0) model.fit(self.x, self.y)
def test_feature_importances(self): model = ComponentwiseGradientBoostingSurvivalAnalysis(n_estimators=100, random_state=0) model.fit(self.x, self.y) self.assertEqual(self.x.shape[1] + 1, len(model.feature_importances_))