PCA-手写字体图片识别
2017-08-23 15:46
609 查看
特征降维
特征降维是无监督学习的另一个应用,有两个目的:
1.会在实际项目中遭遇特征维度非常高的训练样本,往往无法借助自己的领域知识人工构建有效特征;
2.在数据表现方面,无法用肉眼观测超过三个维度的特征。
特征降维不仅重构来有效的低维度特征向量,同时也为数据展现提供了可能。在特征降维的方法中,主成分分析(Principal Component Analysis)是最经典和实用的特征降维技术,特别在辅助图像识别方面有突出表现。
PCA-主成分分析
下面沿用“手写数字图像”的全集数据。
Python源码:
#coding=utf-8
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
#-------------load SVM Classifier based on Linear Kernel
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
#-------------load data
digits_train=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tra',header=None)
digits_test=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tes',header=None)
#sperate 64 dimens picutre pixels features with 1 dimen target number
X_digits=digits_train[np.arange(64)]
y_digits=digits_train[64]
#initialize PCA which can compress 64 dimens feature vector to 2 dimens
estimator=PCA(n_components=2)
X_pca=estimator.fit_transform(X_digits)
def plot_pca_scatter():
colors=['black','blue','purple','yellow','white','red','lime','cyan','orange','gray']
for i in xrange(len(colors)):
px=X_pca[:,0][y_digits.as_matrix()==i]
py=X_pca[:,1][y_digits.as_matrix()==i]
plt.scatter(px,py,c=colors[i])
plt.legend(np.arange(0,10).astype(str))
plt.xlabel('First Principal Component')
plt.ylabel('Second Principal Component')
plt.show()
#show the compressed 2 dimens distribution
plot_pca_scatter()
#-------------train
#sperate 64 dimens picutre pixels features with 1 dimen target number
X_train=digits_train[np.arange(64)]
y_train=digits_train[64]
X_test=digits_train[np.arange(64)]
y_test=digits_train[64]
#train on 64 dimens
svc=LinearSVC()
svc.fit(X_train,y_train)
y_predict=svc.predict(X_test)
#compress from 64 dimens to 20 dimens
estimator=PCA(n_components=20)
pca_X_train=estimator.fit_transform(X_train)
pca_X_test=estimator.fit_transform(X_test)
#train on 20 dimens
pca_svc=LinearSVC()
pca_svc.fit(pca_X_train,y_train)
pca_y_predict=pca_svc.predict(pca_X_test)
#-------------performance measure
print 'Accuracy on 64 dimens:',svc.score(X_test,y_test)
print classification_report(y_test,y_predict,target_names=np.arange(10).astype(str))
print 'Accuracy on 20 dimens:',pca_svc.score(pca_X_test,y_test)
print classification_report(y_test,pca_y_predict,target_names=np.arange(10).astype(str))
经过PCA处理之后,数字图像映射在二维空间的分布情况如图。尽管把原始的六十四维度的图像压缩到只有两个维度的特征空间,依然可以发现大多数数字之间的区分性。
分布训练两个以支持向量机(分类)基础的手写体数字图像识别模型,其中一个模型使用原始六十四维度的像素特征,另一个采用经过PCA压缩重建之后的低维特征。
显示手写数字图片经过PCA压缩后的二维空间分布
尽管经过PCA特征压缩和重建之后的数据特征会损失2%左右的预测准确性,但是相比于原始数据六十四维度的特征而言,使用PCA压缩降低了68.75%的维度
特点分析:降维/压缩问题是选取数据具有代表性的特征,在保持数据多样性(Variance)的基础上,规避掉大量的特征冗余和噪声,这个过程也很有可能会损失一些有用的模式信息。经过大量的实践证明,相较于损失的少部分模型性能,维度压缩能够节省大量用于模型训练的时间。这样一来,使得PCA所带来的模型综合效率变得更为划算。
特征降维是无监督学习的另一个应用,有两个目的:
1.会在实际项目中遭遇特征维度非常高的训练样本,往往无法借助自己的领域知识人工构建有效特征;
2.在数据表现方面,无法用肉眼观测超过三个维度的特征。
特征降维不仅重构来有效的低维度特征向量,同时也为数据展现提供了可能。在特征降维的方法中,主成分分析(Principal Component Analysis)是最经典和实用的特征降维技术,特别在辅助图像识别方面有突出表现。
PCA-主成分分析
下面沿用“手写数字图像”的全集数据。
Python源码:
#coding=utf-8
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
#-------------load SVM Classifier based on Linear Kernel
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
#-------------load data
digits_train=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tra',header=None)
digits_test=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tes',header=None)
#sperate 64 dimens picutre pixels features with 1 dimen target number
X_digits=digits_train[np.arange(64)]
y_digits=digits_train[64]
#initialize PCA which can compress 64 dimens feature vector to 2 dimens
estimator=PCA(n_components=2)
X_pca=estimator.fit_transform(X_digits)
def plot_pca_scatter():
colors=['black','blue','purple','yellow','white','red','lime','cyan','orange','gray']
for i in xrange(len(colors)):
px=X_pca[:,0][y_digits.as_matrix()==i]
py=X_pca[:,1][y_digits.as_matrix()==i]
plt.scatter(px,py,c=colors[i])
plt.legend(np.arange(0,10).astype(str))
plt.xlabel('First Principal Component')
plt.ylabel('Second Principal Component')
plt.show()
#show the compressed 2 dimens distribution
plot_pca_scatter()
#-------------train
#sperate 64 dimens picutre pixels features with 1 dimen target number
X_train=digits_train[np.arange(64)]
y_train=digits_train[64]
X_test=digits_train[np.arange(64)]
y_test=digits_train[64]
#train on 64 dimens
svc=LinearSVC()
svc.fit(X_train,y_train)
y_predict=svc.predict(X_test)
#compress from 64 dimens to 20 dimens
estimator=PCA(n_components=20)
pca_X_train=estimator.fit_transform(X_train)
pca_X_test=estimator.fit_transform(X_test)
#train on 20 dimens
pca_svc=LinearSVC()
pca_svc.fit(pca_X_train,y_train)
pca_y_predict=pca_svc.predict(pca_X_test)
#-------------performance measure
print 'Accuracy on 64 dimens:',svc.score(X_test,y_test)
print classification_report(y_test,y_predict,target_names=np.arange(10).astype(str))
print 'Accuracy on 20 dimens:',pca_svc.score(pca_X_test,y_test)
print classification_report(y_test,pca_y_predict,target_names=np.arange(10).astype(str))
经过PCA处理之后,数字图像映射在二维空间的分布情况如图。尽管把原始的六十四维度的图像压缩到只有两个维度的特征空间,依然可以发现大多数数字之间的区分性。
分布训练两个以支持向量机(分类)基础的手写体数字图像识别模型,其中一个模型使用原始六十四维度的像素特征,另一个采用经过PCA压缩重建之后的低维特征。
显示手写数字图片经过PCA压缩后的二维空间分布
尽管经过PCA特征压缩和重建之后的数据特征会损失2%左右的预测准确性,但是相比于原始数据六十四维度的特征而言,使用PCA压缩降低了68.75%的维度
特点分析:降维/压缩问题是选取数据具有代表性的特征,在保持数据多样性(Variance)的基础上,规避掉大量的特征冗余和噪声,这个过程也很有可能会损失一些有用的模式信息。经过大量的实践证明,相较于损失的少部分模型性能,维度压缩能够节省大量用于模型训练的时间。这样一来,使得PCA所带来的模型综合效率变得更为划算。
相关文章推荐
- caffe 从mnsit示例到自己创建数据集再到识别手写图片
- 03:一文全解:使用Tensorflow搭建卷积神经网络CNN识别手写数字图片
- tensorflow的mnist手写字体识别
- 5/12 cnn 手写字体识别代码学习笔记
- Tensorflow 实现MNIST手写数字体识别
- Python实现识别手写数字 Python图片读入与处理
- 深度学习与TensorFlow实战(七)全连接网络基础—真实图片输出手写数字识别准确率
- Error Back Propagation in BPNeuralNetwork及手写字体识别python版
- 利用caffe训练好的模型测试自己的手写字体图片
- PK/NN/*/SVM:实现手写数字识别(数据集50000张图片)比较3种算法神经网络、灰度平均值、SVM各自的准确率—Jason niu
- cnn 手写字体识别的matlab代码概括
- 基于kNN的手写字体识别——《机器学习实战》笔记
- 02:一文全解:利用谷歌深度学习框架Tensorflow识别手写数字图片(初学者篇)
- 机器学习-神经网络-手写字体识别
- Python实现识别手写数字 简易图片存储管理系统
- tensorflow Lenet5手写字体识别模型的保存与加载
- GAN生成手写字体识别
- Tensorflow MINIST数据模型的训练,保存,恢复和手写字体识别
- 关于matlab中的手写字体识别 的mat文件
- 【Tensorflow入门】手写字体识别(MNIST)