自己实现KNN算法
Posted Erick - LONG
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己实现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]
以上是关于自己实现KNN算法的主要内容,如果未能解决你的问题,请参考以下文章