KNN算法学习笔记

#-*-coding:utf-8-*-
from numpy import *
import operator

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

#KNN分类器
def classify0(inx,dataSet,labels,k):
    dataSetSize = dataSet.shape[0]
    diffMat = tile(inx,(dataSetSize,1))-dataSet
    # 计算点到每个点的距离
    sqDiffMat=diffMat**2
    sqDistances = sqDiffMat.sum(axis=1)
    distances= sqDistances**0.5
    #print (dataSetSize,diffMat,sqDistances,distances)

    sorteDistIndicies=distances.argsort()
   # print (sorteDistIndicies)
    classCount={}  #字典
    for i in range(k):
        voteIlabel = labels[sorteDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel,0)+1
    #print (classCount)

    sortedClassCount= sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
    return sortedClassCount[0][0]

#读取文件
def file2matrix(filename):
    fr= open(filename)
    arrayOline = fr.readlines()
    numberOfLines = len (arrayOline)
    returnMat=zeros((numberOfLines,3))
    classLabelVector = []
    index = 0
    for line in arrayOline:
        line =line.strip()
        listFormLine = line.split('\t')
        returnMat[index,:]=listFormLine[0:3]
        classLabelVector.append(int(listFormLine[-1]))
        index += 1
    return returnMat,classLabelVector

#数值归一化
def autoNorm(dataSet):
    minVals=dataSet.min(0)
    maxVals=dataSet.max(0)
    ranges =maxVals - minVals
    normDataSet=zeros(shape(dataSet))
    m = dataSet.shape[0]
    normDataSet = dataSet -tile(minVals,(m,1))
    normDataSet = normDataSet/tile(ranges,(m,1))
    return normDataSet,ranges,minVals

#测试
def datingClassTest(hoRatio,k):
    #hoRatio =0.10
    datingDataMat,datingLabels =file2matrix('datingTestSet2.txt')
    normMat,ranges,minVals = autoNorm(datingDataMat)
    m = normMat.shape[0]
    numTestVecs = int(m*hoRatio)
    errorCount = 0.0
    for i in range(numTestVecs):
        classifierResult = classify0(normMat[i,:],normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],k)
        print "the classfier came back with :%d,the real answer id %d "%(classifierResult,datingLabels[i])
        if(classifierResult != datingLabels[i]):
            errorCount += 1.0
    print "the total error rate id :%f "%(errorCount/float(numTestVecs))

#预测
def classifyPerson():
    resultList =['not at all','in small doses','in large doses']
    percentTats = float(raw_input("percentage of time spent playing video games?"))
    ffmiles = float(raw_input("frequent flier miles earned per year ?"))
    iceCream = float(raw_input("liters of ice Cream consumed per year ?"))
    datingDataMat,datingLabels =file2matrix('datingTestSet2.txt')
    normMat,ranges,minVals = autoNorm(datingDataMat)
    inArr = array ([ffmiles,percentTats,iceCream])
    classifierResult= classify0((inArr-minVals)/ranges,normMat,datingLabels,3)
    print "you will like this person:",resultList[classifierResult - 1] #  resultListlist为0,1,2 classifierResult 是1,2,3
group,labels=createDataSet()
print group,labels
print classify0([0.5,0.6],group,labels,3)
datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')
datingDataMat
array([[  4.09200000e+04,   8.32697600e+00,   9.53952000e-01],
       [  1.44880000e+04,   7.15346900e+00,   1.67390400e+00],
       [  2.60520000e+04,   1.44187100e+00,   8.05124000e-01],
       ..., 
       [  2.65750000e+04,   1.06501020e+01,   8.66627000e-01],
       [  4.81110000e+04,   9.13452800e+00,   7.28045000e-01],
       [  4.37570000e+04,   7.88260100e+00,   1.33244600e+00]])
datingDataMat[1,:]
array([  1.44880000e+04,   7.15346900e+00,   1.67390400e+00])
import matplotlib 
import matplotlib.pyplot as plt
fig=plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1],datingDataMat[:,2])
plt.show
<function matplotlib.pyplot.show>

这里写图片描述

fig=plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1],datingDataMat[:,2],15.0*array(datingLabels),15.0*array(datingLabels))
plt.show
<function matplotlib.pyplot.show>

png

fig=plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,0],datingDataMat[:,1],15.0*array(datingLabels),15.0*array(datingLabels))
plt.show
<function matplotlib.pyplot.show>

这里写图片描述

fig=plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,0],datingDataMat[:,2],15.0*array(datingLabels),15.0*array(datingLabels))
plt.show
<function matplotlib.pyplot.show>

png

normMat,ranges,minVals = autoNorm(datingDataMat)
normMat
array([[ 0.44832535,  0.39805139,  0.56233353],
       [ 0.15873259,  0.34195467,  0.98724416],
       [ 0.28542943,  0.06892523,  0.47449629],
       ..., 
       [ 0.29115949,  0.50910294,  0.51079493],
       [ 0.52711097,  0.43665451,  0.4290048 ],
       [ 0.47940793,  0.3768091 ,  0.78571804]])
ranges
array([  9.12730000e+04,   2.09193490e+01,   1.69436100e+00])
datingClassTest(0.1,3)
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 3 
the classfier came back with :1,the real answer id 1 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 3 
the classfier came back with :3,the real answer id 3 
the classfier came back with :2,the real answer id 2 
the classfier came back with :1,the real answer id 1 
the classfier came back with :3,the real answer id 1 
the total error rate id :0.050000
classifyPerson()
percentage of time spent playing video games?10
frequent flier miles earned per year ?1000
liters of ice Cream consumed per year ?0.5
you will like this person: in small doses
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值