# 给大家分享一篇 从零开始构造邻近分类器KNN

## 前置阅读

import math
def euler_distance(point1: list, point2: list) -> float:
"""
计算两点之间的欧拉距离，支持多维
"""
distance = 0.0
for a, b in zip(point1, point2):
distance += math.pow(a - b, 2)
return math.sqrt(distance)

## KNN 分类器

import collections
import numpy as np
class KNeighborsClass(object):
def __init__(self, n_neighbors=5):
self.n_neighbors = n_neighbors

def fit(self, data_set, labels):
self.data_set = data_set
self.labels = labels

def predict(self, test_row):
dist = []
for v in self.data_set:
dist.append(euler_distance(v, test_row))
dist = np.array(dist)
sorted_dist_index = np.argsort(dist) # 根据元素的值从大到小对元素进行排序，返回下标

# 根据K值选出分类结果, ['A', 'B', 'B', 'A', ...]
class_list = [ self.labels[ sorted_dist_index[i] ] for i in range(self.n_neighbors)]
result_dict = collections.Counter(class_list)   # 计算各个分类出现的次数
ret = sorted(result_dict.items(), key=lambda x: x[1], reverse=True) # 采用多数表决，即排序后的第一个分类
return ret[0][0]

## 测试

if __name__ == "__main__":
from sklearn import datasets
print(predict)  # output: 1  
Python学习交流群：834179111，群里有很多的学习资料。欢迎欢迎各位前来交流学习。