首页 > 其他 > 详细

[MachineLearning]KNN

时间:2014-06-21 13:29:56      阅读:356      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 18 11:46:15 2014

@author: hp
"""

import numpy as np
import operator

def createDataSet():
    group=np.random.rand(4,2)
    labels=[‘a‘,‘b‘,‘c‘,‘d‘]
    return group,labels
    
def classify0(inX,dataSet,labels,k):
    dataSetSize=dataSet.shape[0]
    diffMat=np.tile(inX,(dataSetSize,1))-dataSet
    sqDiffMat=diffMat**2
    sqDistances=sqDiffMat.sum(axis=1)
    distances=sqDistances**0.5
    sortedDistIndicies=distances.argsort()
    classCount={}
    for i in range(k):
        voteLabel=labels[sortedDistIndicies[i]]
        classCount[voteLabel]=classCount.get(voteLabel,0)+1
    sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
    return sortedClassCount[0][0]
    
group,labels=createDataSet()
result=classify0([0,1],group,labels,3)
print ‘对坐标0,0的分类为%s‘%(result)

  

[MachineLearning]KNN,布布扣,bubuko.com

[MachineLearning]KNN

原文:http://www.cnblogs.com/colipso/p/3794724.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!