机器学习实战学习1:python实现KNN

python实现KNN:

1.KNN.py

# KNN method
'''
step1:calculate distance between input and given datasets
step2:sort and find the nearest k distances
step3:find the class which occupy the most

Notice:if KNN revised,reload,the run KNN_test
'''

from numpy import *
import operator

def CreateDataset():
    group=array([[1.0,0.9],[1.0,1.0],[0.1,0.2],[0.0,0.1]])
    labels=(['A','A','B','B'])
    return group,labels

# KNN_classify
def classify(Input,Dataset,labels,k):
    #step1:calculate distance
    row_number=Dataset.shape[0] #get row numbers
    diffValue=tile(Input,(row_number,1))-Dataset
    diffSquare=diffValue**2
    sumSquare=sum(diffSquare,axis=1)
    distance=sumSquare**0.5

    #step2:sort
    #sort from small to big
    sorted_distance=distance.argsort()

    #step3:find the nearest
    classCount={}
    for i in xrange(k):
        votelabels=labels[sorted_distance[i]]#Notice: use []
        classCount[votelabels]=classCount.get(votelabels,0)+1 #Notice: add [votelabels] after classCount

    maxCount=0
    for key,value in classCount.items(): #Notice: add .items()
        if value>maxCount:
            maxCount=value
            maxIndex=key
    return maxIndex


2.KNN_test.py

from numpy import *
import KNN
test_A=array([1.5,1.2])
test_B=array([0.2,0.5])

Dataset,labels=KNN.CreateDataset()
k=3
output1_label=KNN.classify(test_A,Dataset,labels,k)
output2_label=KNN.classify(test_B,Dataset,labels,k)
print output1_label,output2_label


3.输出:

A,B


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值