<commit_before><commit_msg>created Knn class with a predict method.<commit_after>"""Implement K Nearest Neighbour Classifier in Python."""


class Knn(object):
    """K Nearest Neigbour Classifier.

    Knn.predict(self, data): returns labels for your test data.
    """

    def __init__(self, k=5, dataset=None):
        """Initialize classifier."""
        self.dataset = dataset
        self.k = 5

    def predict(self, data, k=None):
        """Given a data point, predict the class of that data, based on dataset."""
        if not k:
            k = self.k
        for p in data:
            d_list = []
            for q in self.dataset:
                d_list.append((self._distance(p, q), q[-1]))
            neighbors = sorted(d_list)[:k]
            label = self._get_majority_class([x[1] for x in neighbors])
            if len(label) > 1:
                return self.predict(data, k=k - 1)
            return label[0]

    def _get_majority_class(self, classes):
        """Find and return the majority class of a dataset."""
        count = {c: classes.count(c) for c in classes}
        highest = max(count.values())
        return [k for k, v in count.items() if v == highest]

    def _distance(self, p, q):
        """Calculate the distance between two points."""
        import numpy as np
        return np.sqrt(np.sum((p - q)**2))
{'fraction_non_alphanumeric': 0.12269031781226904, 'fraction_numerical': 0.005912786400591279, 'mean_word_length': 2.1488372093023256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3332865', 'n_tokens_mistral': 402, 'n_tokens_neox': 391, 'n_words': 136}