kNN算法python代码学习
来源:互联网 发布:mac 图片处理 除了ps 编辑:程序博客网 时间:2024/05/21 18:42
参考书籍:《机器学习实战》
题目:约会网站
# -*- coding: utf8 -*-from numpy import *import operatordef createDataSet(): group = array([[1.0,1.1], [1.0,1.0], [0,0], [0,0.1]]) labels = ['A','A','B','B'] return group, labelsdef classify0(inX, dataSet, labels, k): #inX是用于分类的输入向量,dataSet是输入的训练样本集,标签向量为labels,参数k表示用于选择最近邻的数目 dataSetSize = dataSet.shape[0] #得到数组的行数,.shape[1]是得到数组的列数 diffMat = tile(inX, (dataSetSize,1)) - dataSet #tile将原来的数组扩展成了四个一样的数组 #tile(inX,(a,b))a代表将原数组高维度扩充(纵向), #b代表低维度扩充(横向),先b后a #diffMat得到了目标与训练数值之间的差值 sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis=1) #每行元素相加(横向),结果转置为行;若axis=0代表每列相加 distances = sqDistances**0.5 sortedDistIndicies = distances.argsort()#升序排列 #选择距离最小的k个点 classCount = {} for i in range(k): voteIlabel = labels[sortedDistIndicies[i]] classCount[voteIlabel] = classCount.get(voteIlabel,0)+1 #字典赋值,dict.get(key,0),key为字典中要查找的键, #如果值不在字典中返回0,default是None #排序 sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True) #sorted(iterable, cmp=None, key=None, reverse=False) #iterable是可迭代类型(如列表),dict.iteritems()产生字典的迭代器 #operator.itemgetter(1)用于获取第1个域的值(从0 开始) #key还可以用lambda表达式来构造 #reverse = True代表降序,False代表默认升序 return sortedClassCount[0][0]
为了将文件里的内容转化为矩阵:
def file2matrix(filename): fr = open(filename) arrayOLines = fr.readlines() numberOfLines = len(arrayOLines) #得到文件的行数 returnMat = zeros((numberOfLines, 3)) #为了简化处理,将矩阵的另一维长度设置为3,根据需求决定 classLabelVector = [] index = 0 for line in arrayOLines: line = line.strip() #去掉所有的回车符 listFromLine = line.split('\t') #将字符串数据用‘\t’分隔成元素列表 returnMat[index,:] = listFromLine[0:3] #将前三个元素作为特征 classLabelVector.append(int(listFromLine[-1])) #可以使用-1索引列表最后一列元素 #如果不告诉解释器为整型,它会将元素当作字符串 index += 1 return returnMat,classLabelVector
用test.py文件进行可视化测试:
import kNNimport matplotlibimport matplotlib.pyplot as pltfrom numpy import arraydef myfind(x,data): return [a for a in range(len(data)) if data[a]==x] datingDataMat,datingLabels = kNN.file2matrix('datingTestSet2.txt')fig = plt.figure()ax = fig.add_subplot(111)set1 = myfind(1,datingLabels)set2 = myfind(2,datingLabels)set3 = myfind(3,datingLabels)plt.plot( [datingDataMat[i,0] for i in set1],[datingDataMat[i,1] for i in set1], 'ro',label = '1') plt.plot( [datingDataMat[i,0] for i in set2],[datingDataMat[i,1] for i in set2],'bo', label = '2')plt.plot( [datingDataMat[i,0] for i in set3],[datingDataMat[i,1] for i in set3],'ko', label = '3')#为了解决legend的问题!!!!ax.set_xlabel('flying km per year')ax.set_ylabel('the percentage of playing games')plt.legend(loc = 'best')plt.show()得到的结果为(只利用了三个特征中的两个,分别为每年获得的飞行常客里程数和玩视频游戏所占的百分比):
为了避免绝对数值大的特征对整体样本间距的影响过大(如飞行里程数),所以将特征数值归一化:
def autoNorm(dataSet): minVals = dataSet.min(0)#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)) #newValue = (oldValue-min)/(max-min) return normDataSet, ranges, minVals
接下来,我们需要一个函数对分类器的正确率进行测试。这里构造一个计数器变量,每当对数据进行一次错误分类时,变量的值就加1,程序执行完后计数器的结果除以数据点总数就是错误率。
def datingClassTest(): hoRatio = 0.20#选取的测试样本占总样本的百分比 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],3) #各参数的含义见classify0函数定义。我们选取了前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))
利用此函数测试一下:
>>> reload(kNN)<module 'kNN' from 'C:\Users\mrzhang\Desktop\prac\python\kNN.py'>>>> kNN.datingClassTest()
得到的结果为:
the classifier came back with: 3, the real answer is: 3the 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: 1, the real answer is: 1......省略一部分数据the classifier came back with: 3, the real answer is: 3the classifier came back with: 2, the real answer is: 2the classifier came back with: 1, the real answer is: 1the classifier came back with: 3, the real answer is: 1the total error rate is: 0.050000
错误率为5%, 通过改变类别数目和测试样本数目,这个数字会改变。
接下来,我们可以利用此分类器,通过输入未知对象的属性信息,由分类软件来判定Helen和某一对象的可交往程度:讨厌、一般喜欢、非常喜欢。
对未知对象的预测函数为:
def classifyPerson(): resultList = ['not at all','in small doses', 'in large doses'] percentTats = float( raw_input("persentage of time spent playing video games?")) ffMiles = float( raw_input('frequent flier miles earned peryear?')) 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 probably like this person:',resultList[classifierResult - 1]
我们来试验一下:
>>> kNN.classifyPerson()persentage of time spent playing video games?20frequent flier miles earned peryear?5000liters of ice cream consumed per year?1You will probably like this person: in large doses
0 0
- kNN算法python代码学习
- KNN算法--python实现代码
- kNN算法python代码学习2-手写识别系统
- 机器学习 Python kNN算法
- python KNN算法 机器学习
- kNN算法的python代码和注释
- KNN算法原理(python代码实现)
- 机器学习实战:KNN 代码注释(python)
- 1、机器学习算法KNN -- Java代码
- 机器学习-KNN算法代码详解
- Python学习系列1:KNN算法
- Python机器学习(二)--kNN算法实现
- 机器学习之PYTHON实现KNN算法
- python 机器学习之kNN算法
- Python机器学习实战kNN分类算法
- python 机器学习KNN分类算法
- 【机器学习】使用python实现kNN算法
- 机器学习KNN python算法实现
- Hibernate多对多双向关联
- 扩展BaseAdapter实现Adapter
- vc2010编译错误atlcore.h(35): fatal error C1189: #error
- Linux 下C++开发环境的搭建
- HTML学习 - 打地鼠
- kNN算法python代码学习
- MyEclipse 2015 上配置CSS3.0和HTML5的标签自动提示
- 黑马程序员---前序《对部分基础知识的查漏补缺》
- iOS开发 大文件下载(支持断点下载)
- 安卓应用程序的多语言配置
- 关于equals的解说
- 64位win7使用debug的方法
- 用NetBeans实现表格的增加、删减、修改功能。
- I/O字节流和字符流的使用