coding utf8 base class for nearest neighbors classifier and regressor parameters k int default 5 the number of neighbors to take into account if 0 all the training examples are used distancefunc function default euclidean distance a distance function taking two arguments any function from scipy spatial distance will do predict the label of a single instance x compute distances between x and all examples in the training set distances self distancefuncx example for example in self x sort all examples by their distance to x and keep their target value neighbors sorteddist target for dist target in zipdistances self y keylambda x x0 get targets of the knn and aggregate them most common one or average neighborstargets target for target in neighbors self k return self aggregateneighborstargets class knnclassifierknnbase return the most common target label mostcommonlabel counterneighborstargets mostcommon100 return mostcommonlabel class knnregressorknnbase return the mean of all targets return np meanneighborstargets coding utf 8 base class for nearest neighbors classifier and regressor parameters k int default 5 the number of neighbors to take into account if 0 all the training examples are used distance_func function default euclidean distance a distance function taking two arguments any function from scipy spatial distance will do l none returns the whole list predict the label of a single instance x compute distances between x and all examples in the training set sort all examples by their distance to x and keep their target value get targets of the k nn and aggregate them most common one or average nearest neighbors classifier note if there is a tie for the most common label among the neighbors then the predicted label is arbitrary return the most common target label nearest neighbors regressor return the mean of all targets
from collections import Counter

import numpy as np
from scipy.spatial.distance import euclidean

from mla.base import BaseEstimator


class KNNBase(BaseEstimator):
    def __init__(self, k=5, distance_func=euclidean):
        

        self.k = None if k == 0 else k  
        self.distance_func = distance_func

    def aggregate(self, neighbors_targets):
        raise NotImplementedError()

    def _predict(self, X=None):
        predictions = [self._predict_x(x) for x in X]

        return np.array(predictions)

    def _predict_x(self, x):
        

        
        distances = (self.distance_func(x, example) for example in self.X)

        
        neighbors = sorted(((dist, target) for (dist, target) in zip(distances, self.y)), key=lambda x: x[0])

        
        
        neighbors_targets = [target for (_, target) in neighbors[: self.k]]

        return self.aggregate(neighbors_targets)


class KNNClassifier(KNNBase):
    

    def aggregate(self, neighbors_targets):
        

        most_common_label = Counter(neighbors_targets).most_common(1)[0][0]
        return most_common_label


class KNNRegressor(KNNBase):
    

    def aggregate(self, neighbors_targets):
        

        return np.mean(neighbors_targets)