# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from numpy import *
#测试数据集
def creatdataset():
group=array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels=['A','A','B','B']
return group,labels
#K邻近算法
def classify0(inx,dataset,labels,k):
datasetsize=dataset.shape[0]#求列数
diffmat=tile(inx,(datasetsize,1))-dataset#求测试数据与训练数据的差
sqdiffmat=diffmat**2#平方
sqdistance=sqdiffmat.sum(axis=1)#求和
distance=sqdistance**0.5#开方,这几步是计算欧氏距离
suoyinpm=distance.argsort()#对距离进行排序
biaoqiandict={}#新建一个字典用来存储标签
for i in range(k):#选取最近的K个数据
biaoqianweizhi=labels[suoyinpm[i]]#根据距离排序的索引得到相应位置的标签
biaoqiandict.setdefault(biaoqianweizhi,0)#如果不存在标签,默认0
biaoqiandict[biaoqianweizhi]+=1#存在就加1
zuidabiaoqian=sorted(biaoqiandict.items(),key=lambda x:x[1],reverse=True)
#对字典进行反向排序
return zuidabiaoqian[0][0]#得到最近的标签
#读取文件,整理原始数据格式
def file2matrix(filename):
fr=open(filename)
arrayline=fr.readlines()
n=len(arrayline)
returnmat=zeros((n,3))
classlabel=[]
index=0
for line in arrayline:
line=line.strip()
listline=line.split('\t')
returnmat[index,:]=listline[0:3]
classlabel.append(int(listline[-1]))
index+=1
return returnmat,classlabel
filename='D:/DATA/python/机器学习/machinelearninginaction/Ch02/datingTestSet2.txt'
#dataset,label=file2matrix(filename)
#最大最小数据规范化
def autonorm(dateset):
datamin=dataset.min(0)
datamax=dataset.max(0)
data_max_min=datamax-datamin
normmat=zeros((dataset.shape))
m=dataset.shape[0]
normmat=dataset-tile(datamin,(m,1))
normmat=normmat/tile(data_max_min,(m,1))
return normmat,datamax,datamin
#读取测试数据,运行K邻近算法,计算错误率
def datingclasstext():
dataset,label=file2matrix(filename)
datanorm=autonorm(dataset)
m=datanorm.shape[0]
test=0.1
error=0.0
test_index=int(m*0.1)
for i in range(test_index):
test_label=classify0(datanorm[i,:],datanorm[test_index:,:],label[test_index:],3)
print('测试标签: %d,预测标签: %d' % (label[i],test_label))
if label[i]!=test_label:
error+=1
print ('错误率:%f'%(error/test_index))
#根据输入数据,运行K邻近算法,得到标签
def classifyperson():
resultlist=['一般','有一点魅力','很有魅力']
youxi=float(input('你的游戏时间'))
feixing=float(input('你的飞行时间'))
bingqilin=float(input('你的冰淇淋时间'))
dataset,label=file2matrix(filename)
datanorm,datamax,datamin=autonorm(dataset)
innar=array([feixing,youxi,bingqilin])
classlabel=classify0((innar-datamin)/(datamax-datamin),datanorm,label,3)
print ('你会喜欢这个人:%s'%resultlist[classlabel-1])
#读取原始数据,得到标签
def img2vector(filename):
fr=open(filename)
datamat=zeros((1,1024))
for i in range(32):
line=fr.readline()
for j in range(32):
datamat[0,32*i+j]=int(line[j])
return datamat
#手写数字识别系统
def handwritingclasstest():
hwlabel=[]
trainmulu=listdir('D:/DATA/python/机器学习/machinelearninginaction/Ch02/digits/trainingDigits')
m=len(trainmulu)
train_data=zeros((m,1024))
for i in range(m):
filenametxt=trainmulu[i]
filenametxt=filenametxt.split('.')[0]
filestr=filenametxt.split('_')[0]
train_data[i,:]=img2vector('D:/DATA/python/机器学习/machinelearninginaction/Ch02/digits/trainingDigits/%s'%trainmulu[i])
hwlabel.append(filestr)
testmulu=listdir('D:/DATA/python/机器学习/machinelearninginaction/Ch02/digits/testDigits')
n=len(testmulu)
error=0.0
for i in range(n):
filenametxt1=testmulu[i]
filenametxt1=filenametxt1.split('.')[0]
filestr1=filenametxt1.split('_')[0]
test_data=img2vector('D:/DATA/python/机器学习/machinelearninginaction/Ch02/digits/trainingDigits/%s'%testmulu[i])
classbiaoqian=classify0(test_data,train_data,hwlabel,3)
print('预测标签:%s,实际标签:%s'%(classbiaoqian,filestr1))
if classbiaoqian!=filestr1:error+=1
print ('错误率:%f'%(error/n))
#错误率:0.015856
https://promotion.aliyun.com/ntms/act/enterprise-discount.html?userCode=1qylypgj
【助力企业上云】性能级主机2-5折
阿里云活动,服务器便宜卖哦,通过此链接注册购买,可联系作者协助部署搭建服务器环境。QQ(814341142)