机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

关键字:邻近算法(kNN: k Nearest Neighbors)、python、源码解析、测试
作者:米仓山下
时间:2018-10-21
机器学习实战(Machine Learning in Action,@author: Peter Harrington)
源码下载地址:https://www.manning.com/books/machine-learning-in-action
[email protected]:pbharrin/machinelearninginaction.git

*************************************************************
一、邻近算法(KNN)

原理:存在一个样本数据集合(训练样本),并且样本集中每个数据都存在标签(知道每个样本所属类别),输入没有标签的新数据(测试数据),将新数据的每个特征与样本集中数据对应的特征进行比较,然后算法提取样本集中特征最相似数据(最邻近)的分类标签,一般只选择k个最相似的数据,通常k不超过20的整数,最后选择k个最相似数据中出现次数最多的分类,作为新数据的分类。

#KNN实现函数
Input:inX: 待分类数据向量(1xN); dataSet:已知分类的m条数据集(NxM);labels:数据类别标签(1xM vector);k:KNN的最邻近数据个数阈值
Output:最可能的类别标签

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                     #计算欧式距离
    sortedDistIndicies = distances.argsort()         #排序  
    classCount={}          
    for i in range(k):                               #计算k个数据中,各类别出现的次数
        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
    sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)#排序
    return sortedClassCount[0][0]                    #返回出现次数最多的类别

*************************************************************
二、kNN.py中其他方法

classify0----KNN实现函数
createDataSet----创建测试数据集,4×2,两个特征,四条数据;标签为1×4,对应为类别
file2matrix----读取数据创建为32×32的矩阵,用来处理手写体数字识别的数据处理,将文本文件中的数据处理成矩阵。
autoNorm----归一化函数:(value-minValue)/(maxValue-minValue),将传入的数据集进行归一化处理,归一化将所有特征的取值压缩到0-1之间(或-1到1之间),可以消除不同特征取值在不同数量级所造成的不同特征重要性不均等的情况。

#约会数据测试
datingClassTest----已完成分类的数据为datingTestSet2.txt中。
数据说明:前三列为特征——每年获得的飞行常客里程数,玩视频游戏所耗时间百分比,每周消费冰淇淋公升数;最后一类为类别——不喜欢的人,魅力一般的人,极具魅力的人。
通过hoRatio来控制训练和测试数据比例,classifierResult为测试所得的类别,方法中还通过与真实类别比较,然后统计错误率和错误数
------------------------------------------------------------
#手写体数字识别(0-9)
handwritingClassTest----手写体识别数字示例,已知类别(0-9十个数字)数据,已经处理成32×32的0-1矩阵数据。
数据说明:训练数据位于trainingDigits文件夹内,里面的文本文件命名规则为:"_"之前为该文件实际对应的数字;每个文件里面为32×32的矩阵,通过0-1来表示对应的数据

img2vector----2×32的0-1矩阵数据文件读取后转换为1×1024的向量

------------------------------------------------------------
测试:
import kNN
>>> kNN.datingClassTest()#约会数据测试
the classifier came back with: 3, the real answer is: 3
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: 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

>>> kNN.handwritingClassTest()#手写体数字识别(0-9)
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9
                        ……
the classifier came back with: 9, the real answer is: 9
the classifier came back with: 9, the real answer is: 9

the total number of errors is: 11

the total error rate is: 0.011628

*************************************************************
三、利用matplotlib绘制散点图

python createFirstPlot.py

(图datingTestSet2)
python createDist2.py

(图createDist2)
*************************************************************

总结:方法无法持久化,每次测试都要计算一遍

猜你喜欢

转载自www.cnblogs.com/Micang/p/9900800.html