K-NN(最近邻分类算法 python
# algorithm:K-NN(最近邻分类算法)
# author:Kermit.L
# time: 2016-8-7
#==============================================================================
from numpy import *
import operator
import matplotlib.pyplot as plt
def creatDataSet():
group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group,labels
group,labels = creatDataSet()
plt.figure(1)
plt.plot(group[:,0],group[:,1],'or')
plt.xlim(-0.5, 1.5)
plt.ylim(-0.5, 1.5)
plt.grid()
for i in xrange(group.shape[0]):
plt.text(group[i][0]-0.06,group[i][1],labels[i])
plt.show()
def classify0(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inX,(dataSetSize,1)) - dataSet;
sqDiffMat = diffMat ** 2
sqDistance = sqDiffMat.sum(axis = 1)
distance = sqDistance ** 0.5
sortDistanceIndex = distance.argsort()
classCount ={}
for i in xrange(k):
voteLabel = labels[sortDistanceIndex[i]]
classCount[voteLabel] = classCount.get(voteLabel,0) + 1
sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter,
reverse = True)
return sortedClassCount[0][0]
# print sortDistanceIndex
# sqDiffMat = diffMat.sum(axs = 1)
# print sqDiffMat
# distance = sqDiffMat ** 0.5
inX =[0.8,0.6]
print classify0(inX, group, labels, 2)
最新文章
- MATLAB-RSP 随笔
- 一个新人如何学习在大型系统中添加新功能和Debug
- jQuery-1.9.1源码分析系列(七) 钩子(hooks)机制及浏览器兼容
- JavaScript高级-定义函数(类)方法
- TreeSet集合深入了解--------攻击原理
- A trip through the Graphics Pipeline 2011_09_Pixel processing – “join phase”
- linux 搭建SVN服务器,为多个项目分别建立版本库并单独配置权限
- 【BZOJ】【1055】【HAOI2008】玩具取名
- HTML之一字符集
- Java通过JDBC连接Oracle之后查询结果和在sqlplus查询结果不一样
- jsoup技术抓取网页数据大全
- Linux-手动释放缓存(Buffer、Cache)
- JSON多层数据添加与访问
- logging的使用方法
- android java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
- python 小技巧
- enctype=“multipart/form-data”详解
- SSL/TLS中间人攻击
- centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
- AWK 知识库