python使用knn实现特征向量分类

yipeiwu_com5年前Python基础

这是一个使用knn把特征向量进行分类的demo。

Knn算法的思想简单说就是:看输入的sample点周围的k个点都属于哪个类,哪个类的点最多,就把sample归为哪个类。也就是说,训练集是一些已经被手动打好标签的数据,knn会根据你打好的标签来挖掘同类对象的相似点,从而推算sample的标签。

Knn算法的准确度受k影响较大,可能需要写个循环试一下选出针对不同数据集的最优的k。

至于如何拿到特征向量,可以参考之前的博文。

代码:

#-*- coding: utf-8 -*-
__author__ = 'Rossie'
from numpy import *
import operator

'''构造数据'''
def createDataSet():
  characters=array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
  labels=['A','A','B','B']
  return characters,labels

'''从文件中读取数据,将文本记录转换为矩阵,提取其中特征和类标'''
def file2matrix(filename):
  fr=open(filename)
  arrayOLines=fr.readlines()
  numberOfLines=len(arrayOLines)    #得到文件行数
  returnMat=zeros((numberOfLines,3))   #创建以零填充的numberOfLines*3的NumPy矩阵
  classLabelVector=[]
  index=0
  for line in arrayOLines:       #解析文件数据到列表
    line=line.strip()
    listFromLine=line.split('\t')
    returnMat[index, :]=listFromLine[0:3]
    classLabelVector.append(listFromLine[-1])
    index+=1
  return returnMat,classLabelVector   #返回特征矩阵和类标集合

'''归一化数字特征值到0-1范围'''
'''输入为特征值矩阵'''
def autoNorm(dataSet):
  minVals=dataSet.min(0)
  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 classify(sample,dataSet,labels,k):
  dataSetSize=dataSet.shape[0]   #数据集行数即数据集记录数
  '''距离计算'''
  diffMat=tile(sample,(dataSetSize,1))-dataSet     #样本与原先所有样本的差值矩阵
  sqDiffMat=diffMat**2   #差值矩阵平方
  sqDistances=sqDiffMat.sum(axis=1)    #计算每一行上元素的和
  distances=sqDistances**0.5  #开方
  sortedDistIndicies=distances.argsort()   #按distances中元素进行升序排序后得到的对应下标的列表
  '''选择距离最小的k个点'''
  classCount={}
  for i in range(k):
    voteIlabel=labels[sortedDistIndicies[i]]
    classCount[voteIlabel]=classCount.get(voteIlabel,0)+1
  '''从大到小排序'''
  sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
  return sortedClassCount[0][0]

'''针对约会网站数据的测试代码'''
def datingClassTest():
  hoRatio=0.20     #测试样例数据比例
  datingDataMat,datingLabels=file2matrix('datingTestSet1.txt')
  normMat, ranges, minVals=autoNorm(datingDataMat)
  m =normMat.shape[0]
  numTestVecs=int(m*hoRatio)
  errorCount=0.0
  k=4
  for i in range(numTestVecs):
    classifierResult=classify(normMat[i, : ],normMat[numTestVecs:m, : ],datingLabels[numTestVecs:m],k)
    print("The classifier came back with: %s, thereal answer is: %s" %(classifierResult, datingLabels[i]))
    if(classifierResult!= datingLabels [i] ) :
      errorCount += 1.0
  print("the total error rate is: %f" % (errorCount/float(numTestVecs)))

def main():
  sample=[0,0]#简单样本测试
  sampleText = [39948,6.830795,1.213342]#文本中向量样本测试
  k=3
  group,labels=createDataSet()
  label1=classify(sample,group,labels,k)#简单样本的分类结果
  fileN = "datingTestSet.txt"
  matrix,label = file2matrix(fileN)
  label2 =classify(sampleText,matrix,label,k)#文本样本的分类结果
  print("ClassifiedLabel of the simple sample:"+label1)
  print("Classified Label of the textsample:"+label2)



if __name__=='__main__':
  main()
  #datingClassTest()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python出现"IndentationError: unexpected indent"错误解决办法

python出现"IndentationError: unexpected indent"错误解决办法

python出现"IndentationError: unexpected indent"错误解决办法 Python是一种对缩进非常敏感的语言,最常见的情况是tab和空格的混用会导致错误...

Flask之flask-script模块使用

Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用...

python 动态加载的实现方法

脚本语言都有一个优点,就是动态加载。lua语言有这个优点,python也有这个特性。说简单点就是,如果开发者发现自己的代码有bug,那么他可以在不关闭原来代码的基础之上,动态替换模块。替...

python 缺失值处理的方法(Imputation)

一、缺失值的处理方法 由于各种各样的原因,真实世界中的许多数据集都包含缺失数据,这些数据经常被编码成空格、nans或者是其他的占位符。但是这样的数据集并不能被scikit - learn...

Python 实现一个颜色色值转换的小工具

Python 实现一个颜色色值转换的小工具

  需求说明   公司的 UI 设计小哥,已经转用 Zeplin 很久了。Zeplin 的设计稿展示页面的颜色色值使用十进制的 RGB 表示的,在 Android 中的颜色表示大多情况下...