PCA和Softmax分类比较—Mnist与人脸数据集
PCA人脸识别中三种方法得到的正确率可达到100%
作为对比,单独使用Softmax回归对人脸40*10*92*112的数据分类正确率为97%。
用PCA对MNIST手写数字10*500*28*28识别,也可以达到相对比较高的正确率,马氏距离h=32时正确率为0.93 (在softmax中为0.85~0.89)。
# coding:utf8
import numpy as np
import os
import sf
import pca if __name__ == '__main__':
img=pca.load_img()
test=img
print np.mat(img).shape
label=[a+1 for a in range(40) for j in range(10)]
index=range(400)
np.random.shuffle(index)
label_=[label[i] for i in index]
test_=np.mat([test[i] for i in index]) softmax = sf.SoftMax(MAXT=200, step=0.03, landa=0.01)
softmax.process_train(np.mat(img),np.array(label),40)
softmax.validate(test_,np.array(label_))
# correctnum = 390, sumnum = 400, Accuracy:0.97
#coding:utf8
import cv2
import numpy as np
import matplotlib.pyplot as plt
import cPickle TYPE_NUM=10 #
SAMPLE_NUM=500 # def load_img():
img=[]
for i in range(40):
for j in range(10):
path='att_faces\\s'+str(i+1)+'\\'+str(j+1)+'.pgm'
a=cv2.imread(path,0)
a=a.flatten()/255.0
img.append(a)
return img def dis(A,B,dis_type=0,s=None):
if dis_type==1: # 欧式距离
return np.sum(np.square(A-B))
elif dis_type==2: # 马式距离
f=np.sqrt(abs(np.dot(np.dot((A-B),s.I),(A-B).T))) # h增大时会出现负值
return f.tolist()[0][0]
else: # 曼哈顿距离
return np.sum(abs(A-B)) def pca(data,h,dis_type=0):
q,r=np.linalg.qr(data.T)
u,s,v=np.linalg.svd(r.T)
fi=np.dot(q,(v[:h]).T)
y=np.dot(fi.T,data.T)
ym=[np.mean(np.reshape(x,(TYPE_NUM,SAMPLE_NUM)),axis=1) for x in y]
ym=np.reshape(ym,(h,TYPE_NUM))
c=[]
if dis_type==2:# 计算马氏距离的额外处理"
yr=[np.reshape(x,(TYPE_NUM,SAMPLE_NUM)) for x in y]
yr=[[np.array(yr)[j][k] for j in range(h)]for k in range(TYPE_NUM)]
for k in yr:
k=np.reshape(k,(h,SAMPLE_NUM))
e=np.cov(k)
c.append(e)
return fi,ym,c def validate(fi,ym,test,label,dis_type=0,c=None):
ty=np.dot(fi.T,test.T)
correctnum=0
testnum=len(test)
for i in range(testnum):
if dis_type==2:
n=len(ym.T)
dd=[dis(ty.T[i],ym.T[n_],dis_type,np.mat(c[n_])) for n_ in range(n)]
else:
dd=[dis(ty.T[i],yy,dis_type) for yy in ym.T]
if np.argsort(dd)[0]==label[i]: # mnist中从0开始
correctnum+=1
rate = float(correctnum) / testnum
print "Correctnum = %d, Sumnum = %d" % (correctnum, testnum), "Accuracy:%.2f" % (rate)
return rate if __name__ == '__main__':
f = open('mnist.pkl', 'rb')
training_data, validation_data, test_data = cPickle.load(f)
training_inputs = [np.reshape(x, 784) for x in training_data[0]]
data = np.array(training_inputs[:10000])
training_inputs = [np.reshape(x, 784) for x in validation_data[0]]
vdata = np.array(training_inputs[:5000])
f.close()
label=training_data[1][:10000]
c=np.argsort(label)
l=[label[x] for x in c]
d=[data[x] for x in c]
data_new=[]
label_new=[]
temp=-1000
for i in range(10): # 将数据整理为10类各500个样本依次排列
id= l.index(i)
if id-temp<500:
print "<500"
break
data_new.append(d[id:id+500])
label_new.append(l[id:id+500]) # PCA中不需要,用于在Softmax中验证数据
temp=id
lb=np.array(label_new).flatten()
data_=[]
for j in data_new:
data_+=j
x_=[2**i for i in range(9)]
d_=['Manhattan Distance','Euclidean Metric', 'Mahalanobis Distance']
for j in range(3):
y_=[]
plt.figure()
for i in range(9):
fi,ym,c=pca.pca(np.mat(data_),h=x_[i],dis_type=j)
y_.append(pca.validate(fi,ym,vdata, validation_data[1][:5000],dis_type=j,c=c))
plt.ylim([0,1.0])
plt.plot(x_,y_)
plt.scatter(x_,y_)
plt.xlabel('h')
plt.ylabel('Accuracy')
plt.title(d_[j])
plt.show()
PCA和Softmax分类比较—Mnist与人脸数据集的更多相关文章
- TensorFlow实战第五课(MNIST手写数据集识别)
Tensorflow实现softmax regression识别手写数字 MNIST手写数字识别可以形象的描述为机器学习领域中的hello world. MNIST是一个非常简单的机器视觉数据集.它由 ...
- 利用sklearn对MNIST手写数据集开始一个简单的二分类判别器项目(在这个过程中学习关于模型性能的评价指标,如accuracy,precision,recall,混淆矩阵)
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- matlab练习程序(神经网络识别mnist手写数据集)
记得上次练习了神经网络分类,不过当时应该有些地方写的还是不对. 这次用神经网络识别mnist手写数据集,主要参考了深度学习工具包的一些代码. mnist数据集训练数据一共有28*28*60000个像素 ...
- 机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集
机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集 选自Microsoft www.tz365.Cn 作者:Lee Scott 机器之心编译 参与:李亚洲.吴攀. ...
- 机器学习:PCA(实例:MNIST数据集)
一.数据 获取数据 import numpy as np from sklearn.datasets import fetch_mldata mnist = fetch_mldata("MN ...
- 用Kersa搭建神经网络【MNIST手写数据集】
MNIST手写数据集的识别算得上是深度学习的”hello world“了,所以想要入门必须得掌握.新手入门可以考虑使用Keras框架达到快速实现的目的. 完整代码如下: # 1. 导入库和模块 fro ...
- TensorFlow——MNIST手写数据集
MNIST数据集介绍 MNIST数据集中包含了各种各样的手写数字图片,数据集的官网是:http://yann.lecun.com/exdb/mnist/index.html,我们可以从这里下载数据集. ...
- TensorFlow系列专题(六):实战项目Mnist手写数据集识别
欢迎大家关注我们的网站和系列教程:http://panchuang.net/ ,学习更多的机器学习.深度学习的知识! 目录: 导读 MNIST数据集 数据处理 单层隐藏层神经网络的实现 多层隐藏层神经 ...
- 使用tensorflow的softmax进行mnist识别
tensorflow真是方便,看来深度学习需要怎么使用框架.如何建模- ''' softmax classifier for mnist created on 2019.9.28 author: vi ...
随机推荐
- 配置navigation bar外观
/* 配置navigation bar外观开始 */ self.navigationBar.translucent = YES; self.navigationBar.titleTextAttribu ...
- IOS(SystemConfiguration)框架中关于测试连接网络状态相关方法
1. 在SystemConfiguration.famework中提供和联网相关的function, 可用来检查网络连接状态. 2. SC(SystemConfiguration)框架中关于测试连接网 ...
- Python 的property的实现 .
描述符.就是 将某种特殊类型①的类的实例指派给另一个类的属性 ①只要实现一下三种方法的其中一个就是特殊类型. __get__(self,instance,owner) -用于访问属性,他返回属性的值. ...
- hdu - 2083 - 简易版之最短距离
找到中位数 , 根据对称性 , 当中位数需要两个数取中值的时候不需要取 , 只需要其中的任意一个数几个 例如四个数 1 , 2 , 3 , 4 . 这四个数 , 其中的 2 和 3 都可以 . 然后求 ...
- Bash中的特殊字符
# 表示注释 #! 指定当前脚本的解析器 #!/bin/bash echo "Hello World" ; 命令分隔符 #!/bin/bash echo hello;echo th ...
- 4、SQL基础整理(规范函数)
规范函数: 绝对值 select abs(-5) print abs(-5) 表中取绝对值的方法: select code,name,abs(chinese)as yuwen from xueshen ...
- PHP CI分页类带多个参数
通过修改system中的pagination.php,给每个<a>都增加了class="pagination". view页面 <div class=" ...
- markdown to html
/************************************************************************* * markdown to html * 说明: ...
- jquery ui tab标签
<!DOCTYPE html> <html> <head> <title>tab</title> <style type=" ...
- UVa 10900 - So you want to be a 2n-aire?
题目大意: 一个答题赢奖金的问题,玩家初始的金额为1,给出n,表示有n道题目,t表示说答对一道题目的概率在t到1之间,每次面对一道题,可以选择结束游戏,获得当前奖金:回答下一道问题,答对的概率p在t到 ...