0413学习笔记:实施kNN算法-构建分类器程序

def classify0(inX, dataSet, labels, k): #inX is input vector, dataSet is training set
    dataSetSize = dataSet.shape[0] #calculate distance
    diffMat = tile(inX, (dataSetSize,1)) - dataSet
    sqDiffMat = diffMat ** 2
    sqDistances = sqDiffMat.sum(axis=1)
    distance = sqDistances ** 0.5
    sortedDistIndicies = distance.argsort()
    classCount = {}
    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
    sortedClassCount = sorted(classCount.iteritems(),
                              key = operator.itemgetter(1),reverse = True)
    return sortedClassCount[0][0]

测试数据所在分类过程:

import sys
sys.path.append("/home/yang/Software/pycharm-community-2017.3.4/bin/桌面/PycharmProjects/untitled/k18")
import kNN
group, labels = kNN.createDataSet()
kNN.classify0([0,0], group, labels, 3)

 Out[7]: 'B'

阅读更多
想对作者说点什么?

博主推荐

换一批

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