kNN分类算法
import numpy as np import operator def createDataSet(): ''' 创建数据集 :return: 数据集特征值,数据集标签 ''' group = np.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): ''' :param inX: 输入向量,即待测试数据 :param dataset: 数据集特征值 :param labels: 数据集标签 :param k: 取最近的k个值 :return: 标定结果 ''' datasetSize = dataset.shape[0] #数组行数 diffMat = np.tile(inX, (datasetSize, 1)) - dataset #输入向量行数乘以datasetSize倍-dataset sqDiffMat = diffMat ** 2 #求每一项的平方 sqDistances = sqDiffMat.sum(axis=1) #每一行的和 distances = sqDistances ** 0.5 # 对sqDistances开方 sortedDistIndicies = distances.argsort() #对distances中元素从小到大排序,返回对应原数组中的索引 classCount = {} for i in range(k): voteIlabel = labels[sortedDistIndicies[i]] classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True) #对字典按值从大到小排序 return sortedClassCount[0][0] if __name__ == '__main__': group,labels = createDataSet() print(classify0([1,1], group, labels, 3)) #输入向量为[1,1] print(classify0([0,0], group, labels, 3)) # 输入向量为[0,0]
运行结果:
E:\Anaconda3\python.exe E:/kNN.py
A
B
进程已结束,退出代码 0
摘自《机器学习实战》