当前位置: 代码迷 >> 综合 >> python sklearn NMF人脸识别
  详细解决方案

python sklearn NMF人脸识别

热度:12   发布时间:2023-12-08 23:55:47.0

非负矩阵分解(NMF,Non-negative Matrix Factorization)

非负矩阵分解是在矩阵中所有元素均为非负数约束条件之下的矩阵分解方法
基本思想:给定一个非负矩阵V,NMF能够找到一个百负矩阵W和一个非负矩阵H,使得矩阵W和H的乘积近似等于矩阵V中的值V=W*H
W矩阵:基础图像矩阵,相当于从原矩阵V中抽取出来的特征
V矩阵:系数矩阵
NMF能够广泛应用于图像分析、文本挖掘和语音等领域

矩阵分解优化目标:最小化W矩阵和H矩阵的乘积和原始矩阵之间的差别

http://blog.csdn.net/acdreamers/article/details/44663421/

主要参数:

dp.NMF(n_components=None,init=None,solver=‘cd’,beta_loss=‘frobenius’,tol=1e-4,
max_iter=200,random_state=None,alpha-0.,l1_ratio=0.,verbose=0,shuffle=False)
n_components:用于指定分解后矩阵的单个维度k
init:W矩阵和H矩阵的初始化方式,默认为’nndsvdar’

NMF人脸数据特征提取

目标:已知Olivetti人脸数据共400个每个数据是64*64大小,由于NMF分解得到的W矩阵相当于从原始矩阵中提取的特征,
那么就可以使用NMF对400个人脸数据提行特征提取
通过设置k的大小,设置提取的特征数目,在本实验中设置k=6,随后将提取的特征以图像的形式展示出来

人脸特征提取实例:

import sklearn.decomposition as dp
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces
from numpy.random import RandomState #创建随机种子
n_row,n_col=2,3
n_components=n_row*n_col
image_shape=(64,64)
datasets=fetch_olivetti_faces(shuffle=True,random_state=RandomState(0))
#dataset=fetch_olivetti_faces(data_home=None,shuffle=False,random_state=0,download_if_missing=True)
faces=datasets.data #加载工打开数据def plot_gallery(title,images,n_col=n_col,n_row=n_row):plt.figure(figsize=(2.*n_col,2.26*n_row)) #创建图片,并指定图片大小plt.suptitle(title,size=18) #设置标题及字号大小for i,comp in enumerate(images):plt.subplot(n_row,n_col,i+1) #选择绘制的子图vmax=max(comp.max(),-comp.min())plt.imshow(comp.reshape(image_shape),cmap=plt.cm.gray,interpolation='nearest',vmin=-vmax,vmax=vmax) #对数值归一化,并以灰度图形式显示plt.xticks(())plt.yticks(()) #去除子图的坐标轴标签plt.subplots_adjust(0.01,0.05,0.99,0.94,0.04,0.) #对子图位置及间隔调整plot_gallery('First centered Olivetti faces',faces[:n_components])
estimators=[('Eigenfaces-PCA using randomized SVD',dp.PCA(n_components=6,whiten=True)),('Non-negative components - NMF',dp.NMF(n_components=6,init='nndsvda',tol=5e-3))] #NMFPCA实例化for name,estimator in estimators: #分别调用PCANMFestimator.fit(faces) #调用PCANMF提取特征components_=estimator.components_ #获取提取的特征plot_gallery(name,components_[:n_components]) #按照固定格式进行排列
plt.show()

学习目标:https://blog.csdn.net/qq_32892383/article/details/91347164