机器学习实战python版归一化数值和测试kNN算法以及构建完整可用系统
来源:互联网 发布:票房数据排行榜 编辑:程序博客网 时间:2024/05/18 03:14
经过前面的学习,我们已经能够较为熟练的应用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 matplotlibimport matplotlib.pyplot as pltimport kNNdatingDataMat,datingLabels = kNN.file2matrix('datingTestSet2.txt')normMat,ranges,minVals = kN.autoNorm(datingDataMat)>>> normMatarray([[ 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]])>>> rangesarray([ 9.12730000e+04, 2.09193490e+01, 1.69436100e+00])>>> minValsarray([ 0. .001156])sarray([ 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: 1the classifier came back with: 1, the real answer is: 1the classifier came back with: 1, the real answer is: 1the classifier came back with: 3, the real answer is: 3the classifier came back with: 1, the real answer is: 1the classifier came back with: 2, the real answer is: 1the classifier came back with: 2, the real answer is: 2the classifier came back with: 1, the real answer is: 1the classifier came back with: 1, the real answer is: 1the classifier came back with: 2, the real answer is: 2the total error rate is: 0.06400032.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
到目前为止,我们已经看到如何在数据上构建分类器了,但是这里的数据比较简单,我们自己看起来都比较容易分开
接下来,我们就要在不太容易看出来的数据上构建分类器!
接下来要学习手写识别系统!请大家多多指教!
- 机器学习实战python版归一化数值和测试kNN算法以及构建完整可用系统
- Python机器学习实战kNN分类算法
- 机器学习算法——归一化数值
- python 机器学习实战KNN
- 机器学习实战-KNN算法
- 机器学习实战 KNN算法
- 《机器学习实战》-- KNN算法
- 机器学习实战 kNN算法
- 机器学习实战-KNN算法
- 机器学习实战--KNN算法
- 机器学习实战-KNN 算法
- 机器学习实战:KNN算法
- 机器学习实战-KNN算法
- 【机器学习算法-python实现】矩阵去噪以及归一化
- Python机器学习实战之kNN手写识别系统
- 机器学习实战2:k近邻算法KNN(python)
- Python机器学习实战笔记之KNN算法
- 机器学习实战——python实现knn算法
- Python练手(一)
- Spring RESTful服务接收和返回JSON最佳实践
- test
- 【Leetcode】Merge k Sorted Lists
- Semaphore使用
- 机器学习实战python版归一化数值和测试kNN算法以及构建完整可用系统
- win7系统下安装VS2010和IIS7问题汇总
- POJ 3259 *** Wormholes
- linux的文件结构详解
- Service was originally bound here
- 社会生活十大著名法则
- 对办公文件的操作
- JavaService将jar发布为windows 服务
- Linux 内核引导参数简介