机器学习实战python版归一化数值和测试kNN算法以及构建完整可用系统

经过前面的学习,我们已经能够较为熟练的应用python而且能够比较轻松的阅读书中的代码。
第一博机器学习实战python环境搭建以及numpy和matplotlib安装遇到的各种问题(一)
第二博机器学习实战python版本matplotlib安装遇到的各种问题和代码演示

学习了几天的机器学习实战,现在已经完全不可收拾了,昨天加班搞完手头的事,今天就迫不及待的打开课本继续欣赏书中算法和代码了。

归一化数值:

通过学习我们发现,数据中数值大的属性对结果的影响最大,而实际中每一个属性是同等重要的,我们我们要进行归一化,确保每个属性所占的比重相同,我们用的方法就是:
找到最大值,最小值,然后用现在的数据值减去最小值,再除以幅度值。

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))   #element wise divide
    #再除以幅度值,实现归一化,tile功能是创建一定规模的指定数组
    return normDataSet, ranges, minVals

如上面所示我已经做了自己的注释,也方便自己以后看
的时候好理解。运行下面的代码就可以看到归一化的数据了:

import matplotlib
import matplotlib.pyplot as plt
import kNN


datingDataMat,datingLabels = kNN.file2matrix('datingTestSet2.txt')
normMat,ranges,minVals = kN.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])
>>> minVals
array([ 0.     .001156])

s
array([ 0.     .001156])

测试算法:

在给的数据中,我们用90%的数据去训练分类器,用10%的数据去测试分类器,检测分类器的正确率。

def datingClassTest():
    hoRatio = 0.50      #hold out 10%
    datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')       #load data setfrom file
    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],3)
        #normMat[i,:]即为前numTestVecs行的数据,后面为比对数据
        print "the classifier came back with: %d, the real answer is: %d" % (classifierResult, datingLabels[i])
        if (classifierResult != datingLabels[i]): errorCount += 1.0
        #如果测试数据的结果和数据资料中应有的结果不一致则错误标记加一。
    print "the total error rate is: %f" % (errorCount/float(numTestVecs))
    print errorCount`
`the classifier came back with: 1, the real answer is: 1
the classifier came back with: 1, the real answer is: 1
the classifier came back with: 1, the real answer is: 1
the classifier came back with: 3, the real answer is: 3
the classifier came back with: 1, the real answer is: 1
the classifier came back with: 2, the real answer is: 1
the classifier came back with: 2, the real answer is: 2
the classifier came back with: 1, the real answer is: 1
the classifier came back with: 1, the real answer is: 1
the classifier came back with: 2, the real answer is: 2
the total error rate is: 0.064000
32.0`

使用算法:

经过前面测试,我们得到这个分类器是可以使用的,现在我们需要使用这个分类器,来处理外部输入的数据,实现人机交互

def classifyPerson():
    resultList = ['not at all','in small doses', 'in larfe doses']
    percentTats = float(raw_input("percentage of time playing video games?"))
    ffMiles = float(raw_input("frequent flier miles earned per years?"))
    iceCream = float(raw_input("liters of ice cream consumed per years?"))
    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 probably like this person: ",resultList[classifierResult -1]

这段代码比较简单,raw_input()函数就是用来接收外部输入的数据的,然后再见分类结果输出出来:
>>>
percentage of time playing video games?10
frequent flier miles earned per years?10000
liters of ice cream consumed per years?0.5
you will probably like this person: in small doses

到目前为止,我们已经看到如何在数据上构建分类器了,但是这里的数据比较简单,我们自己看起来都比较容易分开
接下来,我们就要在不太容易看出来的数据上构建分类器!

接下来要学习手写识别系统!请大家多多指教!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值