k近邻 - 改进约会网站的匹配效果
来源:互联网 发布:ubuntu 关闭swap 编辑:程序博客网 时间:2024/05/17 06:00
# -*- coding: utf-8 -*-import numpy as np import operatorfrom os import listdir# k近邻算法实现def classify0(inX, dataSet, labels, k): # 得到数组的行数, 训练数据的大小 dataSetSize = dataSet.shape[0] # 将inX这一个数据, 扩充成dataSetSize个大小一样的数据集 # 减去dataSet, 得到一个差集的数据集 diffMat = np.tile(inX, (dataSetSize, 1)) - dataSet # 平方, 数据集各个元素分别平方 sqDiffMat = diffMat ** 2 # 每行数据求和 sqDistances = sqDiffMat.sum(axis=1) # 开方 distances = sqDistances ** 0.5 # 排序, sortedDistIndicies为得到的索引的序的列表 sortedDistIndicies = distances.argsort() # 字典 classCount = {} for i in range(k): # voteIlabel为得到的label voteIlabel = labels[sortedDistIndicies[i]] # 字典classCount中标签voteIlabel对应的值加一, 0代表初始值从0开始 classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 # classCount.iteritems(): 迭代取出classCount的每个元素 # key: 需要排序的列表项 # reverse: 降序排序 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) return sortedClassCount[0][0]# 文件转成矩阵def file2matrix(filename): # 打开文件 fr = open(filename) # 获取文件的行数 numberOfLines = len(fr.readlines()) # 创建一个行数为:numberOfLines, 列数为3,以0填充的矩阵 returnMat = np.zeros((numberOfLines,3)) classLabelVector = [] fr = open(filename) index = 0 # 循环读取文件的每一行 for line in fr.readlines(): # 去掉每行数据的回车符 line = line.strip() # 将每行数据, 分割成一个元素列表 listFromLine = line.split('\t') # 将每个元素列表的前3个元素, 存到特征矩阵中 returnMat[index,:] = listFromLine[0:3] # 将每个元素列表的最后一个元素, 存到标签向量列表中 classLabelVector.append(int(listFromLine[-1])) # 索引自加 index += 1 # 返回: 特征矩阵, 标签列表 return returnMat,classLabelVectorreturnMat,classLabelVector = file2matrix('datingTestSet2.txt')# 数据归一化# newValue = (oldValue - min)/ (max - min)def autoNorm(dataSet): # 数据中最小值和最大值 minVals = dataSet.min(0) maxVals = dataSet.max(0) # 区间 ranges = maxVals - minVals # np.shape(dataSet): 返回矩阵的行数和列数 # 创建名为normDataSet的用0填充的矩阵 normDataSet = np.zeros(np.shape(dataSet)) # 矩阵的行数 m = dataSet.shape[0] # np.tile(minVals, (m,1): 扩充一个m行1列的用minVals填充的矩阵 # (oldValue - min) normDataSet = dataSet - np.tile(minVals, (m,1)) # (oldValue - min)/ (max - min) normDataSet = normDataSet/np.tile(ranges, (m,1)) return normDataSet, ranges, minValsdef datingClassTest(): hoRatio = 0.5 # 从文件中获取数据 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): # k近邻分类的结果 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]) # 对比k近邻分类的结果和真实的结果 if (classifierResult != datingLabels[i]): errorCount += 1.0 print "the total error rate is: %f" % (errorCount/float(numTestVecs)) print errorCountdatingClassTest()
0 0
- k近邻 - 改进约会网站的匹配效果
- python实现:K近邻算法改进约会网站匹配效果
- K-近邻算法改进约会网站的配对效果
- k-近邻算法改进约会网站的配对效果
- K-近邻:改进约会网站的配对效果
- 使用k-近邻算法改进约会网站的配对效果。
- 机器学习 & python 使用k-近邻算法改进约会网站的配对效果
- k-近邻算法(KNN)--2改进约会网站的配对效果---by香蕉麦乐迪
- 学习笔记:使用k-近邻算法改进约会网站的配对效果
- 【机器学习实战02】使用k-近邻算法改进约会网站的配对效果
- 《机器学习实战》第二章 2.2用k-近邻算法改进约会网站的配对效果
- 机器学习实战—k近邻算法(kNN)02-改进约会网站的配对效果
- 机器学习—使用k-近邻算法改进约会网站的配对效果
- 『机器学习实战』使用 k-近邻算法改进约会网站的配对效果
- 机器学习实战笔记-K近邻算法2(改进约会网站的配对效果)
- 读懂《机器学习实战》代码—K-近邻算法改进约会网站配对效果
- 机器学习实战——K-近邻算法【2:改进约会网站配对效果】
- 机器学习实战——改进约会网站匹配效果
- Gulp构建前后端分离的最佳实践
- Oracle函数(数值函数)
- 不够位数的在前面补0,保留num的长度位数字
- angularJs解决手机端敲空格的时候,出现div问题
- IOT:Azure IOT初试
- k近邻 - 改进约会网站的匹配效果
- 性能优化检测Instruments-time profile的使用
- 运算符重载
- ZOJ - 3705 A - Applications
- QMetaObject::invokeMethod and Q_INVOKABLE
- 高斯模糊Android
- 深度学习的过往
- 简单的读取xml文件
- redis安装、设置密码及开机自启动