欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

机器学习_K近邻Python代码详解

程序员文章站 2022-03-18 14:50:08
k近邻优点:精度高、对异常值不敏感、无数据输入假定;k近邻缺点:计算复杂度高、空间复杂度高import numpy as npimport operatorfrom os import listdir# k近邻分类器def classify0(inx, dataSet, labels, k): da ......
k近邻优点:精度高、对异常值不敏感、无数据输入假定;
k近邻缺点:计算复杂度高、空间复杂度高


import numpy as np
import operator
from os import listdir

# k近邻分类器
def classify0(inx, dataset, labels, k):
datasetsize = dataset.shape[0] # 返回dataset第一维的长度,也就是行数
diffmat = np.tile(inx, (datasetsize, 1))-dataset # tile表示把inx行向量按列方向重复datasetsize次
sqdiffmat = diffmat**2
sqdistances = sqdiffmat.sum(axis=1) # 按列求和
distances = sqdistances**0.5
sorteddistindicies = distances.argsort() # 返回的是数组从小到大的索引值
classcount = {} # 定义一个空字典
for i in range(k):
votelabel = labels[sorteddistindicies[i]] # 返回前k个距离最小的样本的标签值
classcount[votelabel] = classcount.get(votelabel, 0)+1 # get 表示返回指定键的值
# lambda表示输入classcount返回冒号右边的值,reverse=true表示按照降序排列
sortedclasscount=sorted(classcount.items(), key=lambda classcount: classcount[1], reverse=true)
return sortedclasscount[0][0]

# 把.txt文件转换成矩阵形式
def file2matrix(file):
file = open(file) # 返回文件对象
arr = file.readlines() # 返回全部行,是list形式,每一行为list的一个元素
number = len(arr) # 返回对象长度
returnmat = np.zeros((number,3))
index = 0
labelmat = []
for line in arr:
#line = line.strip('\n')
#newline = line.split(' ')
newline = line.strip('\n').split(' ') # 处理逐行数据,strip表示把头尾的'\n'去掉,split表示以空格来分割行数据
# 然后把处理后的行数据返回到newline列表中
returnmat[index,:] = newline[0:3] #表示列表的0,1,2列数据放到index行中
labelmat.append(int(newline[-1]))
index+=1
return returnmat,labelmat

# 归一化
def autonorm(dataset):
minvals = dataset.min(0)

maxvals = dataset.max(0)
ranges = maxvals-minvals
normdataset = np.zeros(np.shape(dataset))
m = normdataset.shape[0]
a = normdataset
a = np.tile(minvals, (m,1))
normdataset = dataset-a
normdataset = normdataset/np.tile(ranges,(m,1))
return normdataset

# 把图像转化成向量的形式
def img2vector(filename):
returnvect = np.zeros((1,1024))
fr = open(filename)
for i in range(32):
linestr = fr.readline() # readline()表示从首行开始,每次读取一行
for j in range(32):
returnvect[0,32*i+j] = int(linestr[j]) #int()函数用于将一个字符串或数字转换成整型
return returnvect # 一张图片转化成一行后的数组

# 手写数字识别系统的测试代码
def handwritingclasstest():
hwlabels = []
trainingfilelist = listdir('e:/workspace/digits/trainingdigits')
m=len(trainingfilelist)
trainingmat = np.zeros((m,1024))
for i in range(m):
filenamestr = trainingfilelist[i] # 例如9_45.txt
filestr = filenamestr.split('.')[0] # split('.')通过.分隔符对字符串进行切片
classnumstr = int(filestr.split('_')[0]) # split('_')通过_分隔符对字符串进行切片
hwlabels.append(classnumstr)
trainingmat[i,:] =img2vector('e:/workspace/digits/trainingdigits/%s' % filenamestr)
testfilelist = listdir('e:/workspace/digits/testdigits')
mtest = len(testfilelist)
errorcount = 0
for i in range(mtest):
filenamestr = testfilelist[i]
filestr = filenamestr.split('.')[0]
classnumstr = int(filestr.split('_')[0])
vectorundertest = img2vector('e:/workspace/digits/testdigits/%s' % filenamestr)
classresult = classify0(vectorundertest,trainingmat,hwlabels,3)
print('the classifier came back with: %d, the real answer is: %d' % (classresult,classnumstr))
if (classresult != classnumstr):
errorcount += 1.0
print('\n the total number of errors is: %d' % (errorcount))
print('\n the total error rate is: %f' % (errorcount/float(mtest)))

handwritingclasstest()