机器学习之k近邻算法

版权声明:本文为博主原创文章,未经博主允许禁止转载(http://blog.csdn.net/napoay) https://blog.csdn.net/napoay/article/details/50358776

这里写图片描述

from numpy import *
import operator

def createDataSet():
    group=array([
          [1.0,1.1],
          [1.0,1.0],
          [0,0],
          [0,0.1],
          [1,0],
          [1.1,0]     
        ])
    labels=['A','A','B','B','C','C']
    return group,labels

def classify0(inX,dataSet,labels,k):
    dataSetSize=dataSet.shape[0]
    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)
    clasCount={}
    for i in range(k):
        print(clasCount)
        voteIlabel=labels[sortedDistIndicies[i]]
        print(voteIlabel)
        print(clasCount.get(voteIlabel,0))
        clasCount[voteIlabel]=clasCount.get(voteIlabel,0)+1
    print(clasCount)
    sortedClassCount=sorted(clasCount.iteritems(),
                     key=operator.itemgetter(1),reverse=True)
    print(sortedClassCount)
    return sortedClassCount[0][0]

group,labels=createDataSet()
print(group)
print(group.shape)
print(group.shape[0])
print(classify0([1,0],group,labels,3))
阅读更多

扫码向博主提问

mydpp

博客专家

熟悉Lucene、ES、ELK
去开通我的Chat快问
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页