《机器学习实战》之k-近邻算法(改进约会网站的配对效果)
示例背景:
准备数据:从文本文件中解析数据
分析数据:使用Matplotlib创建散点图
import matplotlib as mpl
import matplotlib.pyplot as plt
import operator def file2matrix(filename): #获取数据
f = open(filename)
arrayOLines = f.readlines()
numberOfLines = len(arrayOLines)
returnMat = zeros((numberOfLines,3),dtype=float)
#zeros(shape, dtype, order),创建一个shape大小的全为0矩阵,dtype是数据类型,默认为float,
#order表示在内存中排列的方式(以C语言或Fortran语言方式排列),默认为C语言排列
classLabelVector = []
rowIndex = 0
for line in arrayOLines:
line = line.strip()
listFormLine = line.split('\t')
returnMat[rowIndex,:] = listFormLine[0:3]
classLabelVector.append(int(listFormLine[-1]))
rowIndex += 1
return returnMat, classLabelVector if __name__ == "__main__":
datingDataMat, datingLabels = file2matrix('datingTestSet2.txt')
fig = plt.figure() #图
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['font.serif'] = ['KaiTi']
plt.xlabel('玩视频游戏所耗时间百分比')
plt.ylabel('每周消费的冰淇淋公升数')
'''
matplotlib.pyplot.ylabel(s, *args, **kwargs) override = {
'fontsize' : 'small',
'verticalalignment' : 'center',
'horizontalalignment' : 'right',
'rotation'='vertical' : }
'''
ax = fig.add_subplot(111) #将图分成1行1列,当前坐标系位于第1块处(这里总共也就1块)
ax.scatter(datingDataMat[: ,1], datingDataMat[: ,2],15.0*array(datingLabels), 15.0*array(datingLabels))
#scatter是用来画散点图的
# scatter(x,y,s=1,c="g",marker="s",linewidths=0)
# s:散列点的大小,c:散列点的颜色,marker:形状,linewidths:边框宽度
plt.show()

这是简单的创建了一下散点图,可以看到上面的图中还缺少了图例,所以下面的代码以另两列数据为例创建了带图例的散点图,代码大致还是一样的:
from numpy import *
import matplotlib as mpl
import matplotlib.pyplot as plt
import operator def file2matrix(filename): #获取数据
f = open(filename)
arrayOLines = f.readlines()
numberOfLines = len(arrayOLines)
returnMat = zeros((numberOfLines,3),dtype=float)
#zeros(shape, dtype, order),创建一个shape大小的全为0矩阵,dtype是数据类型,默认为float,order表示在内存中排列的方式(以C语言或Fortran语言方式排列),默认为C语言排列
classLabelVector = []
rowIndex = 0
for line in arrayOLines:
line = line.strip()
listFormLine = line.split('\t')
returnMat[rowIndex,:] = listFormLine[0:3]
classLabelVector.append(int(listFormLine[-1]))
rowIndex += 1
return returnMat, classLabelVector if __name__ == "__main__":
datingDataMat, datingLabels = file2matrix('datingTestSet2.txt')
fig = plt.figure() #图
plt.title('散点分析图')
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['font.serif'] = ['KaiTi']
plt.xlabel('每年获取的飞行常客里程数')
plt.ylabel('玩视频游戏所耗时间百分比')
'''
matplotlib.pyplot.ylabel(s, *args, **kwargs) override = {
'fontsize' : 'small',
'verticalalignment' : 'center',
'horizontalalignment' : 'right',
'rotation'='vertical' : }
''' type1_x = []
type1_y = []
type2_x = []
type2_y = []
type3_x = []
type3_y = []
ax = fig.add_subplot(111) #将图分成1行1列,当前坐标系位于第1块处(这里总共也就1块) index = 0
for label in datingLabels:
if label == 1:
type1_x.append(datingDataMat[index][0])
type1_y.append(datingDataMat[index][1])
elif label == 2:
type2_x.append(datingDataMat[index][0])
type2_y.append(datingDataMat[index][1])
elif label == 3:
type3_x.append(datingDataMat[index][0])
type3_y.append(datingDataMat[index][1])
index += 1 type1 = ax.scatter(type1_x, type1_y, s=30, c='b')
type2 = ax.scatter(type2_x, type2_y, s=40, c='r')
type3 = ax.scatter(type3_x, type3_y, s=50, c='y', marker=(3,1)) '''
scatter是用来画散点图的
matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None,**kwargs)
其中,xy是点的坐标,s点的大小
maker是形状可以maker=(5,1)5表示形状是5边型,1表示是星型(0表示多边形,2放射型,3圆形)
alpha表示透明度;facecolor=‘none’表示不填充。
''' ax.legend((type1, type2, type3), ('不喜欢', '魅力一般', '极具魅力'), loc=0)
'''
loc(设置图例显示的位置)
'best' : 0, (only implemented for axes legends)(自适应方式)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
'''
plt.show()
效果还是很不错的:

准备数据:归一化数值
当我们计算样本之间的欧几里得距离时,由于有些数值较大,所以它对结果整体的影响也就越大,那么小数据的可能就毫无影响了。在这个例子中飞行常客里程数很大,然而其余两列数据很小。为了解决这个问题,需要把数据相应的进行比例兑换,也就是这里需要做的归一化数值,将所有数值转化为[0,1]之间的值。
公式为:
$newValue = (oldValue-min)/(max-min)$ ($min$和$max$分别是数据集中的最小特征值和最大特征值)
def autoNorm(dataSet): #归一化数值
minVals = dataSet.min(0) #0表示每列的最小值,1表示每行的最小值,以一维矩阵形式返回
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
测试并构造完整算法
根据这1000个数据,将其中的100个作为测试数据,另900个作为训练集,看着100个数据集的正确率。
最后根据自己输入的测试数据来判断应该出现的结果是什么。
from numpy import *
import matplotlib as mpl
import matplotlib.pyplot as plt
import operator def classify0(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inX, (dataSetSize,1)) - dataSet #统一矩阵,实现加减
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1) #进行累加,axis=0是按列,axis=1是按行
distances = sqDistances**0.5 #开根号
sortedDistIndicies = distances.argsort() #按升序进行排序,返回原下标
classCount = {}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 #get是字典中的方法,前面是要获得的值,后面是若该值不存在时的默认值
sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0] def file2matrix(filename): #获取数据
f = open(filename)
arrayOLines = f.readlines()
numberOfLines = len(arrayOLines)
returnMat = zeros((numberOfLines,3),dtype=float)
#zeros(shape, dtype, order),创建一个shape大小的全为0矩阵,dtype是数据类型,默认为float,order表示在内存中排列的方式(以C语言或Fortran语言方式排列),默认为C语言排列
classLabelVector = []
rowIndex = 0
for line in arrayOLines:
line = line.strip()
listFormLine = line.split('\t')
returnMat[rowIndex,:] = listFormLine[0:3]
classLabelVector.append(int(listFormLine[-1]))
rowIndex += 1
return returnMat, classLabelVector def autoNorm(dataSet): #归一化数值
minVals = dataSet.min(0) #0表示每列的最小值,1表示每行的最小值,以一维矩阵形式返回
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(datingDataMat, datingLabels): #测试正确率
hoRatio = 0.1
m = datingDataMat.shape[0]
numTestVecs = int(hoRatio*m)
numError = 0.0
for i in range(numTestVecs):
classifierResult = classify0(datingDataMat[i,:], datingDataMat[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]):
numError += 1
print('错误率为 %f' %(numError/float(numTestVecs))) def classifyPerson(datingDataMat, datingLabels, ranges, minVals):
result = ['not at all', 'in small doses', 'in large doses']
print('请输入相应信息:')
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?'))
inArr = array([ffMiles, percentTats, iceCream])
classifyResult = classify0((inArr-minVals)/ranges, datingDataMat, datingLabels, 3)
print('You will probably like this person: ', result[classifyResult-1]) if __name__ == "__main__":
datingDataMat, datingLabels = file2matrix('datingTestSet2.txt')
datingDataMat, ranges, minVals = autoNorm(datingDataMat) #归一化数值
datingClassTest(datingDataMat, datingLabels)
classifyPerson(datingDataMat, datingLabels, ranges, minVals)
fig = plt.figure() #图
plt.title('散点分析图')
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['font.serif'] = ['KaiTi']
plt.xlabel('每年获取的飞行常客里程数')
plt.ylabel('玩视频游戏所耗时间百分比')
'''
matplotlib.pyplot.ylabel(s, *args, **kwargs) override = {
'fontsize' : 'small',
'verticalalignment' : 'center',
'horizontalalignment' : 'right',
'rotation'='vertical' : }
''' type1_x = []
type1_y = []
type2_x = []
type2_y = []
type3_x = []
type3_y = []
ax = fig.add_subplot(111) #将图分成1行1列,当前坐标系位于第1块处(这里总共也就1块) index = 0
for label in datingLabels:
if label == 1:
type1_x.append(datingDataMat[index][0])
type1_y.append(datingDataMat[index][1])
elif label == 2:
type2_x.append(datingDataMat[index][0])
type2_y.append(datingDataMat[index][1])
elif label == 3:
type3_x.append(datingDataMat[index][0])
type3_y.append(datingDataMat[index][1])
index += 1 type1 = ax.scatter(type1_x, type1_y, s=30, c='b')
type2 = ax.scatter(type2_x, type2_y, s=40, c='r')
type3 = ax.scatter(type3_x, type3_y, s=50, c='y', marker=(3,1)) '''
scatter是用来画散点图的
matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None,**kwargs)
其中,xy是点的坐标,s点的大小
maker是形状可以maker=(5,1)5表示形状是5边型,1表示是星型(0表示多边形,2放射型,3圆形)
alpha表示透明度;facecolor=‘none’表示不填充。
''' ax.legend((type1, type2, type3), ('不喜欢', '魅力一般', '极具魅力'), loc=0)
'''
loc(设置图例显示的位置)
'best' : 0, (only implemented for axes legends)(自适应方式)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
'''
plt.show()
可以看到错误率为5%:


《机器学习实战》之k-近邻算法(改进约会网站的配对效果)的更多相关文章
- 使用K近邻算法改进约会网站的配对效果
1 定义数据集导入函数 import numpy as np """ 函数说明:打开并解析文件,对数据进行分类:1 代表不喜欢,2 代表魅力一般,3 代表极具魅力 Par ...
- 吴裕雄--天生自然python机器学习:使用K-近邻算法改进约会网站的配对效果
在约会网站使用K-近邻算法 准备数据:从文本文件中解析数据 海伦收集约会数据巳经有了一段时间,她把这些数据存放在文本文件(1如1^及抓 比加 中,每 个样本数据占据一行,总共有1000行.海伦的样本主 ...
- k-近邻(KNN)算法改进约会网站的配对效果[Python]
使用Python实现k-近邻算法的一般流程为: 1.收集数据:提供文本文件 2.准备数据:使用Python解析文本文件,预处理 3.分析数据:可视化处理 4.训练算法:此步骤不适用与k——近邻算法 5 ...
- 机器学习读书笔记(二)使用k-近邻算法改进约会网站的配对效果
一.背景 海伦女士一直使用在线约会网站寻找适合自己的约会对象.尽管约会网站会推荐不同的任选,但她并不是喜欢每一个人.经过一番总结,她发现自己交往过的人可以进行如下分类 不喜欢的人 魅力一般的人 极具魅 ...
- 【Machine Learning in Action --2】K-近邻算法改进约会网站的配对效果
摘自:<机器学习实战>,用python编写的(需要matplotlib和numpy库) 海伦一直使用在线约会网站寻找合适自己的约会对象.尽管约会网站会推荐不同的人选,但她没有从中找到喜欢的 ...
- 使用k-近邻算法改进约会网站的配对效果
---恢复内容开始--- < Machine Learning 机器学习实战>的确是一本学习python,掌握数据相关技能的,不可多得的好书!! 最近邻算法源码如下,给有需要的入门者学习, ...
- 《机器学习实战》-k近邻算法
目录 K-近邻算法 k-近邻算法概述 解析和导入数据 使用 Python 导入数据 实施 kNN 分类算法 测试分类器 使用 k-近邻算法改进约会网站的配对效果 收集数据 准备数据:使用 Python ...
- 机器学习实战1-2.1 KNN改进约会网站的配对效果 datingTestSet2.txt 下载方法
今天读<机器学习实战>读到了使用k-临近算法改进约会网站的配对效果,道理我都懂,但是看到代码里面的数据样本集 datingTestSet2.txt 有点懵,这个样本集在哪里,只给了我一个文 ...
- KNN算法项目实战——改进约会网站的配对效果
KNN项目实战——改进约会网站的配对效果 1.项目背景: 海伦女士一直使用在线约会网站寻找适合自己的约会对象.尽管约会网站会推荐不同的人选,但她并不是喜欢每一个人.经过一番总结,她发现自己交往过的人可 ...
- 02机器学习实战之K近邻算法
第2章 k-近邻算法 KNN 概述 k-近邻(kNN, k-NearestNeighbor)算法是一种基本分类与回归方法,我们这里只讨论分类问题中的 k-近邻算法. 一句话总结:近朱者赤近墨者黑! k ...
随机推荐
- byte以及UTF-8的转码规则
https://www.cnblogs.com/hell8088/p/9184336.html 多年来闲麻烦,只记录笔记,不曾编写BLOG,本文为原创,如需转载请标明出处 废话不说,直奔主题 asci ...
- Modbus库开发笔记之一:实现功能的基本设计(转)
源: Modbus库开发笔记之一:实现功能的基本设计
- css基本知识、选择器
CSS 是指层叠样式表 (Cascading Style Sheets),基本语法规则如下 CSS 由两个主要的部分构成:选择器,以及一条或多条声明 声明以大括号{ }括起来,一个申明包括属性和值,属 ...
- fjwc2019 D1T1 全连(dp+树状数组)
#178. 「2019冬令营提高组」全连 显然我们可以得出一个$O(n^2)$的dp方程 记$f(i)$为取到第$i$个音符时的最大分数,枚举下一个音符的位置$j$进行转移. 蓝后我们就可以用树状数组 ...
- 翻译 Improved Word Representation Learning with Sememes
翻译 Improved Word Representation Learning with Sememes 题目 Improved Word Representation Learning with ...
- golang BDD testcase framework.
BDD What is Behaviour Driven Development and why should I care? Behaviour Driven Development (BDD) i ...
- linux /Android 平台下使用 i2c-tools
下载源码将 i2c-tools 代码下载到 Android 源码的 external 目录下 在 i2c-tools 目录下新建 Android.mk 文件,内容如下: # external/i2c- ...
- Codeforces 844D Interactive LowerBound - 随机化
This is an interactive problem. You are given a sorted in increasing order singly linked list. You s ...
- PID算法控制简单理解
1 传统的位式控制算法 用户期望值Sv(设定值)经控制算法输出一个输出信号OUT,输出信号加载到执行部件上(像MOS管等)对控制对象进行控制(步进电机.加热器等),控制对象的当前值(Pv)如速度通过传 ...
- topcoder srm 683 div1
problem1 link 肯定存在相邻两堆满足不会存在任何操作在这两堆之间进行.然后就成为一条链,那么只需要维护链的前缀和即可判断当前堆和前一堆之间需要多少次操作. problem2 link 对于 ...