def _ensemble_from_candidates(self, db_conn, candidates, y, y_bin):
        """Build an ensemble from a list of candidate models"""

        ensemble = Counter(candidates[:self.n_best])

        ens_score, ens_probs = self._get_ensemble_score(db_conn,
                                                        ensemble,
                                                        y, y_bin)

        ens_count = sum(ensemble.values())
        if (self.verbose):
            sys.stderr.write('%02d/%.3f ' % (ens_count, ens_score))

        cand_ensembles = []
        while(ens_count < self.max_models):
            # compute and collect scores after adding each candidate
            new_scores = []
            for new_model_idx in candidates:
                score, _ = self._score_with_model(db_conn, y, y_bin,
                                                  ens_probs, ens_count,
                                                  new_model_idx)

                new_scores.append({'score': score,
                                   'new_model_idx': new_model_idx})

            new_scores.sort(key=lambda x: x['score'], reverse=True)

            last_ens_score = ens_score
            ens_score = new_scores[0]['score']

            if (self.use_epsilon):
                # if score improvement is less than epsilon,
                # don't add the new model and stop
                score_diff = ens_score - last_ens_score
                if (score_diff < self.epsilon):
                    break

            new_model_idx = new_scores[0]['new_model_idx']
            ensemble.update({new_model_idx: 1})
            _, ens_probs = self._score_with_model(db_conn, y, y_bin,
                                                  ens_probs, ens_count,
                                                  new_model_idx)

            if (not self.use_epsilon):
                # store current ensemble to select best later
                ens_copy = Counter(ensemble)
                cand = {'ens': ens_copy, 'score': ens_score}
                cand_ensembles.append(cand)

            ens_count = sum(ensemble.values())
            if (self.verbose):
                if ((ens_count - self.n_best) % 8 == 0):
                    sys.stderr.write("\n         ")
                msg = '%02d/%.3f ' % (ens_count, ens_score)
                sys.stderr.write(msg)

        if (self.verbose):
            sys.stderr.write('\n')

        if (not self.use_epsilon and ens_count == self.max_models):
            cand_ensembles.sort(key=lambda x: x['score'], reverse=True)
            ensemble = cand_ensembles[0]['ens']

        return ensemble
dataset/ETHPy150Open dclambert/pyensemble/ensemble.py/EnsembleSelectionClassifier._ensemble_from_candidates/original