KNN算法python实现。
原理
- 可参考其它地方
优点
- 算法简单,精度比较高
- 易于理解,易于实现,不需要训练
缺点
- 计算复杂度高,空间复杂度高
- 可解释性比较差
代码
import numpy as np
import operator
def createDataSet():
group = array([[1.0, 1.2], [1.1, 1.0], [0.1, 0.2], [0.0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group, labels
def knn(inx, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inx, (dataSetSize, 1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(1)
distances = sqDistances**0.5
sortedDistIndicies = distances.argsort()
classCount = {}
for i in range(k):
voteLabel = labels[sortedDistIndicies[i]]
if voteLabel not in classCount.keys():
classCount[voteLabel] = 0
classCount[voteLabel] += 1
sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1), reverse = True)
return sortedClassCount[0][0]
测试
group,labels = createDataSet()
knn([0.1,0.2],group,labels,3)
结果
'B'
网友评论