机器学习实战--KNN 算法 笔记

源码部分:

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
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
sortedDisIndicies=distances.argsort()
classCount={}
for i in range(k):
voteIlabel=labels[sortedDisIndicies[i]]
classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0]
def file2matrix(filename): 
fr=open(filename) 
arrayOLines=fr.readlines() 
numberOfLines=len(arrayOLines) 
returnMat=zeros((numberOfLines,3)) 
classLabelVector=[] 
index=0 
for line in arrayOLines:
line=line.strip() 
listFromLine=line.split('\t') 
returnMat[index,:]=listFromLine[0:3] 
classLabelVector.append(int(listFromLine[-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=0.9
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)
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))
def classifyPerson():
resultList=['not at all','in samll doses','in large doses']
percentTats=float(raw_input("percentage of time spent playing video games?"))
ffMiles=floats=float(raw_input("frequent flier miles earned per year?")) 
iceCream=float(raw_input("liters of ice cream consuned 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]


笔记部分: 

1:K-近邻算法是采用测量不同特征值之间的距离方法进行分类。
2:K-近邻算法(KNN)的工作原理:存在一个样本数据集合,也称为训练样本集,并且样本集中每个数据都存在标签,
即我们知道样本中每一个数据与所属分类的对应关系。简单算法训练样本集的定义如下:定义一个KNN模块,
from numpy import * /*导入numpy模块 科学计算包
import operator /*导入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
简单来说,该函数中自定义了一个样本训练集和样本数据标签,然后通过return返回。
3:输入没有标签的新数据后,将新数据的每个特征与样本中数据对应的特征就行比较,然后算法提取样本集中特征最
相似数据(最邻近)的分类标签。一般来说,我们只选择样本数据集中前K个最相似的数据,这就是K-近邻算法中K
的由来,通常K是不大于20的整数。最后,选择K个最相似数据中出现最多次数的分类,作为新数据的分类。
大致算法代码如下:
def classify0(inX,dataSet,labels,k):/* inx为分类的输入向量 dataSet=group labels=labels
dataSetSize=dataSet.shape[0] /*取到训练集的大小 即列数 为4
diffMat=tile(inX,(dataSetSize,1))-dataSet /* tile函数指的是把inX重复dataSetSize次,1表示在列
方向,不带参数表示行方向。numpy.tile函数首先要
sqDiffMat=diffMat**2 导入numpy模块
sqDistances=sqDiffMat.sum(axis=1) /*sum(axis=1)表示按列相加 axis=0表示按行相加
以上三行代码都是计算两点之间距离
distances=sqDistances**0.5
sortedDisIndicies=distances.argsort()/* 升序排列distances内部距离 但是 返回值是从小到大的索
引值 sortedDisIndicies中保存的是distances的编号
不是存的distances中的数据
classCount={}/*定义了一个空字典
for i in range(k):/*产生一个从0到k-1的整数列表 i遍历
voteIlabel=labels[sortedDisIndicies[i]]
classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
sortedClassCount=soted(classCount.iteritems(),key=operator.itemgetter(1),reverse=true)
returen sortedClassCount[0][0] /*返回该字典列表中的第一个 也就是最大的 即出现次数最多的
items() 向下兼容了iteritems() 前者是3.3版本 后者是2.7版本
4:从文本中解析数据 数据集保存在ch02中 文本文档名称为datingTestSet2.txt 前三行分别表示
a:每年获得的飞行常客里程数
b:玩视频游戏所消耗时间百分比
c:每周消耗的冰激凌公升数
d:感兴趣度 
首先,我们将文本记录转换到Numpy的解析程序中
def file2matrix(filename): /*操作文本文档
fr=open(filename) /*打开文档
arrayOLines=fr.readlines() /*读取行数 存在数组中 其中 导入后每行中用\t隔开 两行之间用\n换行
numberOfLines=len(arrayOLines) /*计算多少行
returnMat=zeros((numberOfLines,3)) /*numberoflines行,3列的初始化零的矩阵
classLabelVector=[] /*定义一个空的数组
index=0 /*初始化索引值为零
for line in arrayOLines: /*遍历数组
line=line.strip() /*删除()中的 在此处表示删除空格 删除\n
listFromLine=line.split('\t') /*以\t 分割 并单一保持 
returnMat[index,:]=listFromLine[0:3] /*把a,b,c存入returnMat中的每行 每行中存储三个
classLbelVector.append(int(listFromLine[-1])) /*存储d 在数组中append 添加
index +=1 /*索引自增
return returnMat,classLabelVector /*返回abc 返回感兴趣度 到此文本记录解析完毕 
5:以上我们得到了数据,并且把数据解析,之后我们利用matplotlib进行绘制原始数据的散点图,
首先导入包
import matplotlib
import matplotlib.pyplot as plt
fig=plt.figure() /*建立画板
ax=fig.add_subplot(111) /*添加一个子图
ax.scatter(datingDataMat[:,1],datingDataMat[:,2]) /* scatter 绘制散点图
plt.show() /* 显示
6:类似的我们计算距离,在计算距离中,b的权值过重,我们需要把数值归一化,也就是把所有数据都换算到0-1之间的
小数:
newvalue=(nowvalue-minvalue)/(max-min)
7:在kNN.py中添加一个归一化的函数,这是为了求出的距离对abc的依赖程度相同,而不是过分依赖与b。
def autoNorm(dataSet):
minvals=dataSet.min(0) /*取得最小值 每列的最小值
maxvals=dataSet.max(0)
ranges=maxvals-minvals
normDataSet=zeros(shape(dataSet))/*去dataSet的行数 然后zeros 创建根据该行数的数组 方便保存数据
m=dataSet.shape[0] /*dataSet的行数 
normDataSet=dataSet-tile(minvals,(m,1))
normDataSet=normDataSet/tile(ranges,(m,1))/*归一化最终值
return normDataSet,ranges,minvals
8:在kNN.py中添加一个测试函数,样本集中百分之九十的数据用来训练样本,百分之十的样本用来测试分类器kNN.classify0()。
以下是测试函数:
def datingClassTest()
hoRatio=0.10 /* 百分之十的数据用于测试分类器 更改该变量的值可更改参加测试分类器的数据量
datingDataMat,datingLabels=file2matrix('datingTestSet2.txt')/*导入数据
normMat,ranges,minvals=autoNorm(datingDataMat)/*前三列数据进行归一化
/*这里如果ranges写作range会报错 但是错误会提示在for语句 提示语句是
:numpy.ndarray object is not callable 语句意思是该对象不能重复使用
但是错误是for 语句中range变量在之前被调用了 所以比较难以判断
m=normMat.shape[0] /*得到总行数
numTestVecs=int(m*hoRatio) /* m*hoRatio 是一个浮点型,数组中【A】,A是整数,所以转化成整形
errorCount=0.0 /*初始错误率为0.0
for i in range(numTestVecs): /*设置循环 然后测试分类
classifierReasult=classify0(normMat[i,:],normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],3)
/* 分类器(需要测试的向量,训练样本集(90%),标签集合,K)
print "the classifier came back with: %d,the real answer is:%d" %(classifierReasult,datingLabels[i])
if(classifierReasult!=datingLabels[i]):errorCount+=1.0
print "the total error rate is: %f"%(errorCount/float(numTestVecs)) /* 错误率 
/*最后得出结果 5%的错误率 机器学习实战书上算的2.4% 
/* 在不改变其他值的前提下 改变hoRatio的值 [0.05,0.1,0.2,0.3,0.4,0.5,0.9]对应的错误率[2%,5%,8%,8%,7%,6%,7%] 
9:在测试结束,我们力求得出一个对于未来的约会预测函数,在我们输入飞行里程数,玩视频游戏的百分比和冰激凌公升数,我们希望
得到一个关于这方面预测的结果,我们到底是不是感兴趣他,下面我们给出代码。
def classifyPerson():
resultList=['not at all','in samll doses','in large doses'] /*感兴趣程度
percentTats=float(raw_input("percentage of time spent playing video games?"))
ffMiles=floats=float(raw_input("frequent flier miles earned per year?")) 
iceCream=float(raw_input("liters of ice cream consuned per year?")) /* raw_input 提供了输入功能
datingDataMat,datingLabels=file2matrix('datingTestSet2.txt') /* 导入数据
normMat,ranges,minvals=autoNorm(datingDataMat) /* 归一化 ranges是归一化的分母
inArr=array([ffMiles,percentTats,iceCream]) /* inArr 就是归一化之前的datingDataMat数组中的行
classifierResult=classify0((inArr-minvals)/ranges,normMat,datingLabels,3)/*先归一化 然后调用分类函数
print "you will probably like this person:" resultList[classifierResult-1]


  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值