自己实现KNN算法详解编程语言

import numpy as np 
from math import sqrt 
from collections import Counter 
 
class KNNClassifier(object): 
    """docstring for KNNClassifier""" 
    def __init__(self, k): 
        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): 
        '''根据训练数据集X_train和y_train训练KNN分类器''' 
        self._X_train = X_train 
        self._y_train = y_train 
        return self 
 
    def predict(self,X_predict): 
        y_predict = [self._predict(x) for x in X_predict] 
        return np.array(y_predict) 
 
    def _predict(self,x): 
        distances = [sqrt(np.sum((x_train-x)**2) for x_train in self._X_train)] 
 
        nearest = np.argsort(distances) 
 
        topK_y=[self._y_train[i] for i in nearest[:self.k]] 
        votes = Counter(topK_y) 
 
        return votes.most_common(1)[0][0]

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16219.html

(0)
上一篇 2021年7月19日 19:06
下一篇 2021年7月19日 19:06

相关推荐

发表回复

登录后才能评论