本文主要是介绍简述KNN算法及实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# -*- coding: utf-8 -*-
"""
@author: yb136
"""import numpy as np
import operatordef classify(intX,dataSet,labels,k):#numpy中shape[0]返回数组的行数,shape[1]返回列数dataSetSize = dataSet.shape[0]#将intX在横向重复dataSetSize次,纵向重复1次#例如intX=([1,2])--->([[1,2],[1,2],[1,2],[1,2]])便于后面计算diffMat = np.tile(intX,(dataSetSize,1))-dataSet#二维特征相减后乘方sqdifMax = diffMat**2#计算距离seqDistances = sqdifMax.sum(axis=1)distances = seqDistances**0.5print ("distances:",distances)#返回distance中元素从小到大排序后的索引sortDistance = distances.argsort()print ("sortDistance:",sortDistance)classCount = {}for i in range(k):#取出前k个元素的类别voteLabel = labels[sortDistance[i]]print("第%d个voteLabel=%s" %(i,voteLabel))classCount[voteLabel] = classCount.get(voteLabel,0)+1#dict.get(key,default=None),字典的get()方法,返回指定键的值,如果值不在字典中返回默认值。#计算类别次数#key=operator.itemgetter(1)根据字典的值进行排序#key=operator.itemgetter(0)根据字典的键进行排序#reverse降序排序字典sortedClassCount = sorted(classCount.items(),key = operator.itemgetter(1),reverse = True)print ("sortedClassCount:",sortedClassCount)print (sortedClassCount[0][0])return sortedClassCount[0][0]if __name__ == '__ain__':group = np.array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])labels = ['A','A','B','B']classify([1,1],group,labels,3)
这篇关于简述KNN算法及实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!