def test_fit(self): meta = Stacking(MeanEstimator(), [('coxph', CoxPHSurvivalAnalysis()), ('svm', FastSurvivalSVM(random_state=0))], probabilities=False) self.assertEqual(2, len(meta)) meta.fit(self.x.values, self.y) p = meta._predict_estimators(self.x.values) self.assertTupleEqual((self.x.shape[0], 2), p.shape)
def test_fit_sample_weights(self): data = load_iris() x = data["data"] y = data["target"] meta = Stacking(LogisticRegression(), [('tree', DecisionTreeClassifier(max_depth=1, random_state=0)), ('svm', SVC(probability=True, random_state=0))]) sample_weight = numpy.random.RandomState(0).uniform(size=x.shape[0]) meta.fit(x, y, tree__sample_weight=sample_weight, svm__sample_weight=sample_weight)
def test_score(make_whas500): whas500 = make_whas500(with_mean=False, with_std=False, to_numeric=True) meta = Stacking(MeanEstimator(), [('coxph', CoxPHSurvivalAnalysis()), ('svm', FastSurvivalSVM(random_state=0))], probabilities=False) meta.fit(whas500.x, whas500.y) c_index = meta.score(whas500.x, whas500.y) assert round(abs(c_index - 0.7848807), 5) == 0
def test_fit(make_whas500): whas500 = make_whas500(with_mean=False, with_std=False, to_numeric=True) meta = Stacking(MeanEstimator(), [('coxph', CoxPHSurvivalAnalysis()), ('svm', FastSurvivalSVM(random_state=0))], probabilities=False) assert 2 == len(meta) meta.fit(whas500.x, whas500.y) p = meta._predict_estimators(whas500.x) assert (whas500.x.shape[0], 2) == p.shape
def test_fit(self): data = load_iris() x = data["data"] y = data["target"] meta = Stacking(LogisticRegression(), [('tree', DecisionTreeClassifier(max_depth=1, random_state=0)), ('svm', SVC(probability=True, random_state=0))]) self.assertEqual(2, len(meta)) meta.fit(x, y) p = meta._predict_estimators(x) self.assertTupleEqual((x.shape[0], 3 * 2), p.shape) self.assertTupleEqual((3, 3 * 2), meta.meta_estimator.coef_.shape)
def test_predict(self): meta = Stacking(MeanEstimator(), [('coxph', CoxPHSurvivalAnalysis()), ('svm', FastSurvivalSVM(random_state=0))], probabilities=False) meta.fit(self.x.values, self.y) # result is different if randomForestSRC has not been compiled with OpenMP support p = meta.predict(self.x.values) actual_cindex = concordance_index_censored(self.y['fstat'], self.y['lenfol'], p) expected_cindex = numpy.array([0.7848807, 58983, 16166, 0, 119]) assert_array_almost_equal(expected_cindex, actual_cindex)
def test_predict(self): data = load_iris() x = data["data"] y = data["target"] meta = Stacking(LogisticRegression(multi_class='multinomial', solver='lbfgs'), [('tree', DecisionTreeClassifier(max_depth=1, random_state=0)), ('svm', SVC(probability=True, random_state=0))]) self.assertEqual(2, len(meta)) meta.fit(x, y) p = meta.predict(x) acc = accuracy_score(y, p) self.assertGreaterEqual(acc, 0.98)
def test_predict(make_whas500): whas500 = make_whas500(with_mean=False, with_std=False, to_numeric=True) meta = Stacking(MeanEstimator(), [('coxph', CoxPHSurvivalAnalysis()), ('svm', FastSurvivalSVM(random_state=0))], probabilities=False) meta.fit(whas500.x, whas500.y) # result is different if randomForestSRC has not been compiled with OpenMP support p = meta.predict(whas500.x) assert_cindex_almost_equal(whas500.y['fstat'], whas500.y['lenfol'], p, (0.7848807, 58983, 16166, 0, 14))
def test_predict(): data = load_iris() x = data["data"] y = data["target"] meta = Stacking(LogisticRegression(multi_class='multinomial', solver='lbfgs'), [('tree', DecisionTreeClassifier(max_depth=1, random_state=0)), ('svm', SVC(probability=True, gamma='auto', random_state=0))]) assert 2 == len(meta) meta.fit(x, y) p = meta.predict(x) acc = accuracy_score(y, p) assert acc >= 0.98
def test_fit(): data = load_iris() x = data["data"] y = data["target"] meta = Stacking(LogisticRegression(solver='liblinear', multi_class='ovr'), [('tree', DecisionTreeClassifier(max_depth=1, random_state=0)), ('svm', SVC(probability=True, gamma='auto', random_state=0))]) assert 2 == len(meta) meta.fit(x, y) p = meta._predict_estimators(x) assert (x.shape[0], 3 * 2) == p.shape assert (3, 3 * 2) == meta.meta_estimator.coef_.shape
def test_predict_log_proba(self): data = load_iris() x = data["data"] y = data["target"] meta = Stacking(LogisticRegression(multi_class='multinomial', solver='lbfgs'), [('tree', DecisionTreeClassifier(max_depth=1, random_state=0)), ('svm', SVC(probability=True, random_state=0))]) meta.fit(x, y) p = meta.predict_log_proba(x) scores = numpy.empty(3) for i, c in enumerate(meta.meta_estimator.classes_): scores[i] = roc_auc_score(numpy.asarray(y == c, dtype=int), p[:, i]) assert_array_almost_equal(numpy.array([1.0, 0.9986, 0.9986]), scores)