非负矩阵分解(Non-negative Matrix Factorization ,NMF) 是在矩阵中所有元素均为非负数约束条件之下的矩阵分解方法。
基本思想:给定一个非负矩阵V,NMF能够找到一个非负矩阵W和一个 非负矩阵H,使得矩阵W和H的乘积近似等于矩阵V中的值。
非负矩阵分解
NMF人脸数据特征提取:
代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 26 16:28:29 2017
@author: xiaolian
"""
from numpy.random import RandomState
import matplotlib.pylab as plt
from sklearn.datasets import fetch_olivetti_faces
from sklearn import decomposition
n_row, n_col = 2, 3
n_components = n_row * n_col
image_shape = (64, 64)
# load data
dataset = fetch_olivetti_faces( shuffle = True, random_state = RandomState(0) )
faces = dataset.data
#
def plot_gallery(title, images):
plt.figure(figsize = (2. * n_col, 2.26 * n_row))
plt.suptitle(title, size = 16)
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.93, 0.04, 0.)
plot_gallery( 'first centered olivetti faces', faces[:n_components] )
estimators = [
# name instance
('Eigenfaces - PCA using randomized SVD',
decomposition.PCA(n_components=6,whiten=True)),
('Non-negative components - NMF',
decomposition.NMF(n_components=n_components, init='nndsvda', tol=5e-3))
]
for name, estimator in estimators:
estimator.fit(faces)
components_ = estimator.components_
plot_gallery(name, components_[:n_components])
输出: