def add_sequential(name, models, X_train, Y_train, X_test, Y_test): # Establish and reset variables acc_score_cv = None acc_score = None time_ = None ensemble = SequentialEnsemble(scorer=accuracy_score, random_state=seed) # Add a subsemble with 5 partitions as first layer ensemble.add('subsemble', models, partitions=10, folds=10) # Add a super learner as second layer ensemble.add('stack', models, folds=20) ensemble.add_meta(SVC()) start = time.time() ensemble.fit(X_train, Y_train) preds = ensemble.predict(X_test) acc_score = accuracy_score(preds, Y_test) end = time.time() time_ = end - start return { "Ensemble": name, "Meta_Classifier": "SVC", "Accuracy_Score": acc_score, "Runtime": time_ }
def test_predict(): """[SequentialEnsemble] Test multilayer prediction.""" S = lc_s.predict(X) B = lc_b.predict(S) U = lc_u.predict(B) ens = SequentialEnsemble() ens.add('stack', ESTIMATORS, PREPROCESSING, dtype=np.float64) ens.add('blend', ECM, dtype=np.float64) ens.add('subsemble', ECM, dtype=np.float64) out = ens.fit(X, y).predict(X) np.testing.assert_array_equal(U, out)
def test_equivalence_blend(): """[SequentialEnsemble] Test ensemble equivalence with BlendEnsemble.""" ens = BlendEnsemble() seq = SequentialEnsemble() ens.add(ECM, dtype=np.float64) seq.add('blend', ECM, dtype=np.float64) F = ens.fit(X, y).predict(X) P = seq.fit(X, y).predict(X) np.testing.assert_array_equal(P, F)
def test_equivalence_super_learner(): """[SequentialEnsemble] Test ensemble equivalence with SuperLearner.""" ens = SuperLearner() seq = SequentialEnsemble() ens.add(ECM, dtype=np.float64) seq.add('stack', ECM, dtype=np.float64) F = ens.fit(X, y).predict(X) P = seq.fit(X, y).predict(X) np.testing.assert_array_equal(P, F)
def test_equivalence_subsemble(): """[SequentialEnsemble] Test ensemble equivalence with Subsemble.""" ens = Subsemble(n_jobs=1) seq = SequentialEnsemble(n_jobs=1) ens.add(ECM, dtype=np.float64) seq.add('subsemble', ECM, dtype=np.float64) F = ens.fit(X, y).predict(X) P = seq.fit(X, y).predict(X) np.testing.assert_array_equal(P, F)
def test_equivalence_subsemble(): """[Sequential] Test ensemble equivalence with Subsemble.""" ens = Subsemble() seq = SequentialEnsemble() ens.add(ECM) seq.add('subset', ECM) F = ens.fit(X, y).predict(X) P = seq.fit(X, y).predict(X) np.testing.assert_array_equal(P, F)
def test_predict(): """[Sequential] Test multilayer prediction.""" S = lc_s.predict(X, y) B = lc_b.predict(S, y) U = lc_u.predict(B, y) ens = SequentialEnsemble() ens.add('stack', ESTIMATORS, PREPROCESSING) ens.add('blend', ECM) ens.add('subset', ECM) out = ens.fit(X, y).predict(X) np.testing.assert_array_equal(U, out)