k-近邻算法采用测量不同特征值之间的距离方法进行归类,它属于机器学习算法中的监督学习内容,用于线性回归!
优点:精度高、对异常值不敏感,无数据输入假定。
缺点:计算复杂度高、空间复杂度高
适用数据范围:数值型和标称型。
python实现代码加注释:
#encoding:utf8
from numpy import *
import operator
def createDataSet():
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group, labels
def classify0(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0] #dataSet的行数,等于labels向量的元素数目
diffMat = tile(inX, (dataSetSize, 1)) - dataSet
#print diffMat
sqDiffMat = diffMat**2
#print sqDiffMat
sqDistances = sqDiffMat.sum(axis=1) #计算每行的和
#print sqDistances
distances = sqDistances**0.5 #计算欧式距离
#print distances
sortedDistIndicies = distances.argsort()#返回矩阵中每个元素的排序序号->来自原矩阵的序号:升序排列!!!
#print sortedDistIndicies
classCount = {}#建立一个空字典/哈希表/映射:键值为label;值为每个label出现的频率
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1#统计每个label出现的频率
#D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
sortedClassCount = sorted(classCount.iteritems(), key = operator.itemgetter(1), reverse = True)
#D.iteritems() -> an iterator over the (key, value) items of D
#key = operator.itemgetter(1)->获取函数key第一个域的值
#reverse = True->逆序排序
#将字典分解为列表,
return sortedClassCount[0][0]
group, labels = createDataSet()
print classify0([0,0], group, labels, 3)
运行结果:
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
B
>>>