def build_ensemble(self, X, y, rescore=True):
        """Generate bagged ensemble"""

        self._n_classes = len(np.unique(y))

        db_conn = sqlite3.connect(self.db_file)
        curs = db_conn.cursor()

        # binarize
        if (self._n_classes > 2):
            y_bin = LabelBinarizer().fit_transform(y)
        else:
            y_bin = np.column_stack((1-y, y))

        # get CV scores for fitted models
        if (rescore):
            self._score_models(db_conn, X, y, y_bin)

        # get number of best models to take
        n_models = int(self._n_models * (1.0 - self.prune_fraction))
        bag_size = int(self.bag_fraction * n_models)
        if (self.verbose):
            sys.stderr.write('%d models left after pruning\n' % n_models)
            sys.stderr.write('leaving %d candidates per bag\n\n' % bag_size)

        # get indices and scores from DB
        select_stmt = """select model_idx, score
                         from model_scores
                         order by score desc
                         limit %d"""
        curs.execute(select_stmt % n_models)
        ranked_model_scores = [(r[0], r[1]) for r in curs.fetchall()]

        # print best performing model results
        best_model_score = ranked_model_scores[0][1]
        if (self.verbose):
            self._print_best_results(curs, best_model_score)
            sys.stderr.write("Ensemble scores for each bag (size/score):\n")

        ensembles = []

        # make bags and ensembles
        rs = check_random_state(self.random_state)
        for i in xrange(self.n_bags):
            # get bag_size elements at random
            cand_indices = rs.permutation(n_models)[:bag_size]

            # sort by rank
            candidates = [ranked_model_scores[ci][0] for ci in cand_indices]

            if (self.verbose):
                sys.stderr.write('Bag %02d): ' % (i+1))

            # build an ensemble with current candidates
            ensemble = self._ensemble_from_candidates(db_conn,
                                                      candidates,
                                                      y, y_bin)
            ensembles.append(ensemble)

        # combine ensembles from each bag
        for e in ensembles:
            self._ensemble += e

        # push to DB
        insert_stmt = "insert into ensemble(model_idx, weight) values (?, ?)"
        with db_conn:
            val_gen = ((mi, w) for mi, w in self._ensemble.most_common())
            db_conn.executemany(insert_stmt, val_gen)

        if (self.verbose):
            score, _ = self._get_ensemble_score(db_conn,
                                                self._ensemble,
                                                y, y_bin)

            fmt = "\nFinal ensemble (%d components) CV score: %.5f\n\n"

            sys.stderr.write(fmt % (sum(self._ensemble.values()), score))

        db_conn.close()
dataset/ETHPy150Open dclambert/pyensemble/ensemble.py/EnsembleSelectionClassifier.build_ensemble/original