KNN的python实现

2019-02-19  本文已影响0人  机器学习与自然语言处理
import numpy as np
from math import sqrt
from collections import Counter


def distance(k, X_train, Y_train, x):
    assert 1 <= k <= X_train.shape[0], "K must be valid"
    assert X_train.shape[0] == Y_train.shape[0], "the size of X_train must equal to the size of y_train"
    assert X_train.shape[1] == x.shape[0], "the feature number of x must be equal to X_train"
    distance = [sqrt(np.sum((x_train - x)**2)) for x_train in X_train]
    nearest = np.argsort(distance)
    topk_y = [Y_train[i] for i in nearest[:k]]
    votes = Counter(topk_y)
    return votes.most_common(1)[0][0]


if __name__ == "__main__":
    X_train = np.array([[1.0, 3.5],
                       [2.0, 7],
                       [3.0, 10.5],
                       [4.0, 14],
                       [5, 25],
                       [6, 30],
                       [7, 35],
                       [8, 40]])
    Y_train = np.array([0, 0, 0, 0, 1, 1, 1, 1])
    x = np.array([8, 21])
    label = distance(3, X_train, Y_train, x)
    print(label)

面向对象的knn实现

class KNNClassifier:
    def __init__(self, k):
        """初始化KNN分类器"""
        assert k >= 1, "k must be valid"
        self.k = k
        self._X_train = None
        self._Y_train = None

    def fit(self, X_train, Y_train):
        self._X_train = X_train
        self._Y_train = Y_train
        return self

    def _predict(self,x):
        distances = [sqrt(np.sum((x_train - x) ** 2)) for x_train in self._X_train]
        nearset = np.argsort(distances)
        topK_y = [self._Y_train[i][0] for i in nearset[:self.k]]
        votes = Counter(topK_y)
        return votes.most_common(1)[0][0]

    def predict(self, x_predict):
        y_precict = [self._predict(x) for x in x_predict]
        return np.array(y_precict,dtype=np.int8)

    def __repr__(self):
        return "KNN(k=%d)" %self.k
s = KNNClassifier(k=7)
s.fit(X_train=X_train,Y_train=Y_train)
y = s.predict(x_predict=x,)
print(y[0])
上一篇下一篇

猜你喜欢

热点阅读