机器学习(5)——K-近邻算法【1:从文本中解析数据并可视化】


机器学习实战学习笔记系列

机器学习实战——K-近邻算法【1:从文本中解析数据并可视化】

机器学习实战——K-近邻算法

《机器学习实战》书中代码有一个小错误,直接运行时会报错。
ValueError: invalid literal for int() with base 10: ‘largeDoses’
因为根据源码不能直接读取字符型,应该将测试数据中最后一列字符型改为int型,这里作者已经修改了,并提供了修改后的数据datingTestSet2.txt文件,所以调用测试时
datingDataMat, datingLabels = kNN.file2matrix(‘datingTestSet.txt’)应该改为
datingDataMat, datingLabels = kNN.file2matrix(‘datingTestSet2.txt’).

源代码:

from numpy import *
import operator
from os import listdir

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):
        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]

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 file2matrix(filename):
    fr = open(filename)
    numberOfLines = len(fr.readlines())         #get the number of lines in the file
    returnMat = zeros((numberOfLines,3))        #prepare matrix to return
    classLabelVector = []                       #prepare labels return   
    fr = open(filename)
    index = 0
    for line in fr.readlines():
        line = line.strip()
        listFromLine = line.split('\t')
        returnMat[index,:] = listFromLine[0:3]
        classLabelVector.append(int(listFromLine[-1]))
        index += 1
    return returnMat,classLabelVector

可视化数据

>>> import matplotlib
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
<matplotlib.collections.PathCollection object at 0x00000264FFDE3668>
>>> plt.show()

这里写图片描述

为了标记不同样本的分了,我们可以调用Scatter()模块,通过不同的颜色标记出来。由于Scatter函数中调用了array(),所以我们要首先导入numpy模块

from numpy import *
>>>ax.scatter(datingDataMat[:,1], datingDataMat[:,2], 15.0*array(datingLabels), 15.0*array(datingLabels))
<matplotlib.collections.PathCollection object at 0x00000264FFE16BA8>
>>> plt.show()

 

这里写图片描述

可以看出图中有青色,紫色和黄色三种颜色分别代表了三种类别

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值