网站首页的布局设计,山东百度推广,北京专业网站改版公司,百度seo关键词优化工具人生苦短#xff0c;我用PythonK-近邻算法#xff1a;简单来说#xff0c;K-近邻算法就是采用测量不同特征值之间的距离方法进行分类优点#xff1a;精度高、对异常值不敏感、无数据输入假定缺点#xff1a;计算复杂度高、空间复杂度高适用范围#xff1a;数值型、标称型…人生苦短我用PythonK-近邻算法简单来说K-近邻算法就是采用测量不同特征值之间的距离方法进行分类优点精度高、对异常值不敏感、无数据输入假定缺点计算复杂度高、空间复杂度高适用范围数值型、标称型工作原理存在一个样本数据集合也称作训练样本集并且样本集中每个数据都存在标签即我们知道样本集中每一个数据与所属分类的对应关系。输入没有标签的新数据后将新数据的每个特征与样本集中数据对应的特征进行比较然后算法提取样本集中特征最相似的数据最近邻的分类标签。一般来说我们只选择样本集中前K个最相似的数据这就是K-近邻算法中K的出处通常K是不大于20的整数。最后选择K个最相似数据中出现次数最多的分类作为新数据的分类。K-近邻算法的一般流程收集数据可以使用任何方法。准备数据距离计算所需要的数值最好是结构化的数据格式。分析数据可以使用任何方法。训练算法此步骤不适用于K-近邻算法。测试算法计算错误率。使用算法首先需要输入样本数据和结构化的输出结果然后运行K-近邻算法判定输入数据分别属于哪个分类最后应用对计算出的分类执行后续的处理。实施KNN分类算法--伪代码对未知类别属性的数据集中的每个点依次执行以下操作计算已知类别数据集中的点与当前点之间的距离按照距离递增次序排序选取与当前点距离最小的K个点确定前K个点所在类别的出现频率返回前K个点出现频率最高的类别作为当前点的预测分类计算两个向量点之间的距离公式--欧式距离公式例如点00与12之间的距离计算为sqrt((1-0)**2(2-0)**2)代码实现import numpy as np
import operatordef CreateDataSet():groupnp.array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])labels[A,A,B,B]return group,labels
print(CreateDataSet())inX--用于分类的输入向量
dataSet--输入的训练样本集
labels--标签向量
k--用于选择最近邻居的数目
其中标签向量的元素数目和矩阵dataSet的行数相同def classify(inX,dataSet,labels,k):dataSetSizedataSet.shape[0] #获得训练样本集的行数#将输入向量在列方向重复一次在行方向上dataSize次并与训练样本集dataSet相减diffMatnp.tile(inX,(dataSetSize,1))-dataSetprint(diffMat:)print(diffMat)#将相减后的集合进行平方运算sqDiffMatdiffMat**2print(sqDiffMat:)print(sqDiffMat)#对平方后的集合进行相加运算--按行相加sqDistancessqDiffMat.sum(axis1)print(sqDistances:)print(sqDistances)#对相加后的数据开平方得到输入向量与每个训练样本集之间的距离值distancesnp.sqrt(sqDistances)print(distances)print(distances)#返回数组从小到大的索引值--排序sortedDistIndiciesnp.argsort(distances)print(sortedDistIndicies)print(sortedDistIndicies)classCount{}for i in range(k):voteIlabellabels[sortedDistIndicies[i]]print(voteIlabelstr(i))print(voteIlabel)classCount[voteIlabel]classCount.get(voteIlabel,0)1print(classCountstr(i))print(classCount)sortedClassCountsorted(classCount.items(),keyoperator.itemgetter(1),reverseTrue)print(sortedClassCount:)print(sortedClassCount)return sortedClassCount[0][0]if __name____main__:#训练样本集group np.array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])#标签向量labels [A, A, B, B]#输入向量inX[0,0]#用于选择最近邻居的数目k3resultclassify(inX,group,labels,k)print(result)
输出值
diffMat:
[[-1. -1.1][-1. -1. ][ 0. 0. ][ 0. -0.1]]
sqDiffMat:
[[ 1. 1.21][ 1. 1. ][ 0. 0. ][ 0. 0.01]]
sqDistances:
[ 2.21 2. 0. 0.01]
distances
[ 1.48660687 1.41421356 0. 0.1 ]
sortedDistIndicies
[2 3 1 0]
voteIlabel0
B
classCount0
{B: 1}
voteIlabel1
B
classCount1
{B: 2}
voteIlabel2
A
classCount2
{B: 2, A: 1}
sortedClassCount:
[(B, 2), (A, 1)]
BProcess finished with exit code 0
复制代码测试结果输入[0,0],经过测试后返回的结果是B也就是说[0,0]这个输入向量通过K-近邻算法分类后归为B类示例使用K-近邻算法改进约会网站的配对效果收集数据提供文本文件准备数据使用Python解析文本文件分析数据使用Matplotlib画二维扩散图训练算法此步骤不适用与K-近邻算法测试算法使用海伦提供的部分数据作为测试样本测试样本和非测试的区别在于测试样本是已经完成分类的数据如果预测分类与实际类别不同则标记为一个错误。使用算法产生简单的命令行程序然后可以输入一些特征数据以判断对方是否是自己喜欢的类型准备数据从文本文件中解析数据文本样本数据特征每年获得的飞行常客里程数玩视频游戏所耗时间的百分比每周消费的冰淇淋公升数将文本记录转换为numpy数据的解析程序def file2matrix(filename):# 打开文件fr open(filename, r, encodingutf-8)# 按行读取数据arrayOLines fr.readlines()# 获取数据的行数numberOfLines len(arrayOLines)# 创建以0填充的矩阵returnMat np.zeros((numberOfLines, 3))print(returnMat)classLabelVector []index 0for line in arrayOLines:print(line)# 截取掉所有回车字符line line.strip()print(line)# 以\t将line分割成一个元素列表listFromLine line.split(\t)# 选取前三个元素存储到特征矩阵中returnMat[index, :] listFromLine[0:3]# 选取最后一个元素存储到标签向量中classLabelVector.append(int(listFromLine[-1]))index 1return returnMat, classLabelVector
datingDataMat,datingLabelsfile2matrix(D:\liuguojiang_Python\city_58\city_58\datingTestSet2.txt)
figplt.figure()
plt.title(K-)
plt.xlabel(fly)
plt.ylabel(consume)
axfig.add_subplot(111)ax.scatter(datingDataMat[:,0],datingDataMat[:,1],15.0*np.array(datingLabels),15.0*np.array(datingLabels))
plt.show()复制代码特别说明代码中的资源文件可以在此处下载LiuGuoJiang/machinelearninginaction解析文本数据并用散点图展示准备数据归一化数值任选样本数据中一行数据计算距离时因为飞行常客里程数比较大所以对最后计算结果影响过大所以需要对数据做归一化处理。如将取值范围处理为0~1或者-1~1之间。下面的公式可以将任意取值范围的特征值转化为0~1区间内的值newValue(oldValue-min)/(max-min)其中min和max分别是数据集中的最小特征值和最大特征值。归一化特征值函数def autoNorm(dataSet):#选取列的最小值minValsdataSet.min(0)#选取列的最大值maxValsdataSet.max(0)#列的最大值与最小值做减法rangesmaxVals-minVals#normDataSetnp.zeros([dataSet.shape[0],dataSet.shape[1]])print(normDataSet)#取出dataSet的行数mdataSet.shape[0]#np.tile(minVals,(m,1))将minVals在 列上重复一次在行上重复m次normDataSetdataSet-np.tile(minVals,(m,1)) #oldValue-minnormDataSetnormDataSet/np.tile(ranges,(m,1)) #(oldValue-min)/(max-min)return normDataSet,ranges,minValsnormDataSet,ranges,minValsautoNorm(datingDataMat)
print(normDataSet)复制代码测试算法机器学习算法一个很重要的工作就是评估算法的正确率通常我们只提供已有数据的90%作为训练样本来训练分类器而使用其余的10%数据去测试分类器检测分类器的正确率。10%数据应该是随机选择的。分类器的测试代码def datingClassUnitTest():hoRatio0.10datingDataMat, datingLabels file2matrix(D:\liuguojiang_Python\city_58\city_58\datingTestSet2.txt)print(datingDataMat)normDataSet, ranges, minVals autoNorm(datingDataMat)print(normDataSet)mnormDataSet.shape[0]numTestVecsint(m*hoRatio)print(numTestVecs)print(numTestVecs)errorCount0.0for i in range(numTestVecs):classifierResultclassify(normDataSet[i,:],normDataSet[numTestVecs:m,:],datingLabels[numTestVecs:m],3)print(the classfier came back with:{},the real answer is:{}.format(classifierResult,datingLabels[i]))if (classifierResult!datingLabels[i]):errorCount1.0print(the total error rate is:{}.format(errorCount/float(numTestVecs)))the classfier came back with:3,the real answer is:3
the classfier came back with:2,the real answer is:2
the classfier came back with:1,the real answer is:1
.........
the classfier came back with:1,the real answer is:1
the classfier came back with:3,the real answer is:3
the classfier came back with:3,the real answer is:3
the classfier came back with:2,the real answer is:2
the classfier came back with:1,the real answer is:1
the classfier came back with:3,the real answer is:1
the total error rate is:0.05复制代码分类器处理数据集的错误率是5%即代表此分类器可以帮助对象判定分类。编写可以让用户输入自己需要判断的输入向量通过该分类器帮助用户判断属于哪一分类def classifyPerson():resultList [not at all, in small doses, in large doses]percentTats float(input( \percentage of time spent playing video games?))ffMiles float(input(frequent flier miles earned per year?))iceCream float(input(liters of ice cream consumed per year?))datingDataMat, datingLabels file2matrix(D:\liuguojiang_Python\city_58\city_58\datingTestSet2.txt)normDataSet, ranges, minVals autoNorm(datingDataMat)inArr np.array([ffMiles, percentTats, iceCream, ])classifierResult classify((inArr - \minVals) / ranges, normDataSet, datingLabels, 3)print(You will probably like this person: {}.format(resultList[classifierResult - 1]))
if __name____main__:classifyPerson()
return:
percentage of time spent playing video games?10
frequent flier miles earned per year?10000
liters of ice cream consumed per year?0.5
You will probably like this person: in small doses
复制代码总结定义K-近邻算法程序。定义将文本数据集处理成二维数组的函数便于处理。为消除某一特征数值过大对结果判定的影响定义归一化数值函数公式oldValue-min/(max-min)定义测试算法函数用于测试分类器的错误率是否满足使用要求。定义可以让用户输入的代码输入输入向量用于判定分类