下面,我们用Python实现一个简单的例子
import numpy as pd
import operator
# 创建数据集
def createDataSet():
group = np.array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = [‘A‘, ‘A‘, ‘B‘, ‘B‘]
return group, labels
# k-近邻算法
def classify0(inx, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
# np.tile(inx, (dataSetSize, 1)), 先沿着x轴的方向复制1, 再沿y轴复制dataSetSize
diffMat = np.tile(inx, (dataSetSize, 1)) - dataSet
sqDiffMat = diffMat ** 2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances ** 0.5
# 获取distances从小到大的索引
sortedDistIndicies = distances.argsort()
classCount = {}
# 统计距离最小的k个标签出现的次数
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1
# key=operator.itemgetter(1): 按照值进行排序,降序
sortedclassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)
return sortedclassCount[0][0]
if ‘__name__‘ == ‘__main__‘:
group, labels = createDataSet()
print(classify0([0, 0], group, labels, 3)) # B
**刚刚开始学习,如有错误还请大神可以帮忙指正,更多例子可以参考《机器学习实战》这本书。
原文:https://www.cnblogs.com/xiaohuatongxue/p/12001619.html