KNN算法,我的理解是在有标签的训练样本分布中,有n个需要测试的样本,通过欧拉距离
通过计算每个测试样本得出离训练最近的k个样本,同过k个样本中某一类标签所占的比例最高,则将该测试值预测为该标签。
在sklearn模块中有实现KNN算法实现,直接调用就行了,下面是参考实现的代码:
import numpy as np
from math import sqrt
from collections import Counter
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):
"""根据训练数据集X_train和y_train训练kNN分类器"""
assert X_train.shape[0] == y_train.shape[0], \
"the size of X_train must be equal to the size of y_train"
assert self.k <= X_train.shape[0], \
"the size of X_train must be at least k."
self._X_train = X_train
self._y_train = y_train
return self
def predict(self, X_predict):
"""给定待预测数据集X_predict,返回表示X_predict的结果向量"""
assert self._X_train is not None and self._y_train is not None, \
"must fit before predict!"
assert X_predict.shape[1] == self._X_train.shape[1], \
"the feature number of X_predict must be equal to X_train"
y_predict = [self._predict(x) for x in X_predict]
return np.array(y_predict)
def _predict(self, x):
"""给定单个待预测数据x,返回x的预测结果值"""
assert x.shape[0] == self._X_train.shape[1], \
"the feature number of x must be equal to X_train"
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]#返回一个投票数最高的元组,其中位于第一个最高投票数的对应的label
def score(self, X_test, y_test):
"""根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""
y_predict = self.predict(X_test)
assert len(y_test) == len(y_predict), \
"the size of y_test must be equal to the size of y_predict"
return np.sum(y_test == y_predict) / len(y_test)