Ejemplo n.º 1
0
    def test_can_access_classifier_attribute(self):
        models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
        ensembler1 = Ensembler(models)
        ensembler1.fit(self.X, self.Y)

        est_type = getattr(ensembler1, "_estimator_type")
        self.assertEqual(est_type, 'classifier')
Ejemplo n.º 2
0
    def test_fit_predictproba(self):
        models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
        ensembler1 = Ensembler(models, voting='soft')
        ensembler1.fit(self.X, self.Y)
        pred = ensembler1.predict(self.X)

        acs = accuracy_score(self.Y, pred)
        print('Accuracy Score: ', acs)
Ejemplo n.º 3
0
    def test_fit_base(self):
        models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
        ensembler1 = Ensembler(models)
        ensembler1.fit_base(self.X, self.Y)

        p1 = ensembler1.models[0].predict(self.X)
        self.assertIsInstance(p1, numpy.ndarray)
        p2 = ensembler1.models[1].predict(self.X)
        self.assertIsInstance(p2, numpy.ndarray)
Ejemplo n.º 4
0
    def test_load_saved_ensemble(self):
        models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
        ensembler1 = Ensembler(models)
        ensembler1.fit(self.X, self.Y)
        save_object('ensembler1.pkl', ensembler1)

        # try loading
        ens1 = load_object('ensembler1.pkl')
        self.assertIsInstance(ens1, Ensembler)
Ejemplo n.º 5
0
    def test_object_constructor(self):
        models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
        ensembler1 = Ensembler(models)
        self.assertListEqual(ensembler1.models, models)

        with self.assertRaises(AssertionError) as ae:
            Ensembler(models, None, True)

        with self.assertRaises(AssertionError) as ae:
            Ensembler()
Ejemplo n.º 6
0
    def test_saved_constructor(self):
        rf = RandomForestClassifier(n_estimators=10)
        rf.fit(self.X, self.Y)
        save_object('rf.pkl', rf)
        xg = XGBClassifier()
        xg.fit(self.X, self.Y)
        save_object('xg.pkl', xg)

        es = Ensembler(modelpaths=[
            'rf.pkl',
            'xg.pkl'
        ])
        es.fit(self.X, self.Y)
        preds = es.predict(self.X)
        self.assertIsInstance(preds, numpy.ndarray)
Ejemplo n.º 7
0
    def test_intermediary_state_features(self):
        models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
        ensembler1 = Ensembler(models, voting='hard')
        ensembler1.fit(self.X, self.Y)
        meta_features1 = ensembler1.ensemble.named_steps['base_layer'].transform(self.X)
        print(meta_features1.shape)
        self.assertEqual(meta_features1.shape[0], self.X.shape[0])
        self.assertEqual(meta_features1.shape[1], 2)

        ensembler2 = Ensembler(models, voting='soft')
        ensembler2.fit(self.X, self.Y)
        meta_features2 = ensembler2.ensemble.named_steps['base_layer'].transform(self.X)
        print(meta_features2.shape)
        self.assertEqual(meta_features2.shape[0], self.X.shape[0])
        self.assertEqual(meta_features2.shape[1], 6)
Ejemplo n.º 8
0
 def test_compile(self):
     models = [RandomForestClassifier(n_estimators=10), XGBClassifier()]
     ensembler1 = Ensembler(models)
     ensembler1.fit_base(self.X, self.Y)
     ensemble = ensembler1.compile()
     print(ensemble)