- 假设绿色和蓝色是两个不同的类
- 如下图,红色圆要被决定赋予哪个类,是绿色还是蓝色?如果K=3,由于绿色所占比例为2/3,红色圆将被赋予绿色那个类,如果K=10,由于蓝色比例为7/10,因此红色被赋予蓝色类。
- 由此可见KNN算法很大程度上取决于K的选择。
- 实验流程图:
- 简单KNN算法实现如下:
main.py
#coding:utf-8
import sys
import KNN
from numpy import *
dataSet,labels = KNN.createDataSet()
input = array([0.0, 0.0])
K = 3
output = KNN.classify(input,dataSet,labels,K)
print("测试数据为:",input,"分类结果为:",output)
KNN.py
# coding:utf-8
from numpy import *
import operator
# 给出训练数据以及对应的类别
def createDataSet():
group = array([[0.5, 0.5], [-0.5, 0.5], [-0.2, -0.7], [0.3, 3.5], [1.3, 1.7], [-0.5, 1.8], [-1, 1], [-1.8, -0.2], [-1, -1.8], [0.2, -1.8], [1, -1]])
labels = ['Blue', 'Green', 'Green', 'Blue', 'Blue', 'Blue', 'Blue', 'Blue', 'Green', 'Blue']
return group, labels
# 通过KNN进行分类
def classify(input, dataSet, label, k):
dataSize = dataSet.shape[0]
# 计算欧式距离
diff = tile(input, (dataSize, 1)) - dataSet
sqdiff = diff ** 2
squareDist = sum(sqdiff, axis=1) ###行向量分别相加,从而得到新的一个行向量
dist = squareDist ** 0.5
# 对距离进行排序
sortedDistIndex = argsort(dist) ##argsort()根据元素的值从大到小对元素进行排序,返回下标
classCount = {}
for i in range(k):
voteLabel = label[sortedDistIndex[i]]
# 对选取的K个样本所属的类别个数进行统计
classCount[voteLabel] = classCount.get(voteLabel, 0) + 1
# 选取出现的类别次数最多的类别
maxCount = 0
for key, value in classCount.items():
if value > maxCount:
maxCount = value
classes = key
return classes
测试结果
测试数据为: [0. 0.] 分类结果为: Green