<commit_before># This module contains the class for implementing a hard voting (uses majority classification, not averaging) ensemble.


class HardVotingClassifier:
    def __init__(self, parent_set, scorer_name, scorer_hyperparam, module_mgr):
        self.parent_set = parent_set
        self.scorer_name = scorer_name
        self.scorer_hyperparam = scorer_hyperparam
        self.scorer_func = module_mgr.get_scorer_func(scorer_name)
        self.scores = {}
        self.models = {}<commit_msg>voting classifier (hard). no scoring method yet.<commit_after># This module contains the class for implementing a hard voting (uses majority classification, not average voting)
# ensemble.


class HardVotingClassifier:
    def __init__(self, parent_set, module_mgr):
        self.parent_set = parent_set
        self.module_mgr = module_mgr
        self.scores = {}  # scores dictionary
        self._modelversions = set([])  # contains the ModelVersions that are in the HardVotingClassifier
        self.user_notes = ""
        self.scored = False

    def add_model(self, modelversion):
        self._modelversions.add(modelversion)

    def score(self, scorer_name, scorer_hyperparam):
        pass

    def train(self):
        for modelversion in sorted(self._modelversions):
            if not modelversion.trained_model:
                modelversion.train()

    def predict(self, outside_set):
        for modelversion in sorted(self._modelversions):
            if not modelversion.trained_model:
                print "Not all models in ensemble are trained."
                return None
        classifications = [modelversion.predict(outside_set) for modelversion in sorted(self._modelversions)]
        classifications = get_majority(classifications)
        return classifications


# Sample use case:
# classifications = [["r","y","r","r"],["y","r","r","r"],["y", "y", "y", "y",],["y","y","r","r"]]
# get_majority(classifications) returns ["y","y","r","r"]
def get_majority(classifications):
    new_classifications = []
    for votes in zip(*classifications):
        my_dict = {}
        for vote in votes:
            if vote not in my_dict:
                my_dict[vote] = 1
            else:
                my_dict[vote] += 1
        new_classifications.append(max(my_dict, key=my_dict.get))
    return new_classifications


if __name__ == "__main__":
    print get_majority([["r","y","r","r"],
                        ["y","r","r","r"],
                        ["y","y","y","y"],
                        ["y","y","r","r"]])
{'fraction_non_alphanumeric': 0.11748526522593321, 'fraction_numerical': 0.0007858546168958742, 'mean_word_length': 2.805680119581465, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3491395', 'n_tokens_mistral': 729, 'n_tokens_neox': 709, 'n_words': 179}