NMF方法及實例

非負(fù)矩陣分解(Non-negative Matrix Factorization ,NMF)是在矩陣中所有元素均為非負(fù)數(shù)約束條件下的矩陣分解方法。
基本思想:給定一個非負(fù)矩陣V,NMF能夠找到一個非負(fù)矩陣W和一個非負(fù)矩陣H,使得矩陣W和H的乘積近似等于矩陣V中的值。


image.png

? W矩陣:基礎(chǔ)圖像矩陣,相當(dāng)于從原矩陣V中抽取出來的特征
? H矩陣:系數(shù)矩陣。
? NMF 能夠廣泛應(yīng)用于圖像分析、文本挖掘和語音處理等領(lǐng)域

image.png
image.png
image.png
image.png

在sklearn庫中,可以使用sklearn.decomposition.NMF加載NMF算法,主要參數(shù)有:
n_components:用于指定分解后矩陣的單個維度k;
init:W矩陣和H矩陣的初始化方式,默認(rèn)為'nndsvdar'

NMF人臉數(shù)據(jù)特征提取
目標(biāo):已知Olivetti人臉數(shù)據(jù)共400個,每個數(shù)據(jù)是64*64大小。由于NMF分解得到的W矩陣相當(dāng)于從原始矩陣中提取的特征,那么就可以使用NMF對400個人臉數(shù)據(jù)進行特征提取

image.png

通過設(shè)置 k的 大小,設(shè)置提取特征的數(shù)目。在本實驗中設(shè)置 k=6,隨后將提取的特征以圖像的形式展示出來。

image.png
image.png
image.png
image.png
image.png

image.png

runfile('C:/Users/Sean Chang/.spyder/nmf.py', wdir='C:/Users/Sean Chang/.spyder')
Extracting the top 9 Eigenfaces - PCA using randomized SVD...
(400L, 4096L)
Extracting the top 9 Non-negative components - NMF...
(400L, 4096L)

# -*- coding: utf-8 -*-
"""
Created on Tue May 23 18:03:38 2017

@author: Sean Chang
"""

from numpy.random import RandomState
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces
from sklearn import decomposition
 
 
n_row, n_col = 3, 3
n_components = n_row * n_col
image_shape = (64, 64)
 
 
###############################################################################
# Load faces data
dataset = fetch_olivetti_faces(shuffle=True, random_state=RandomState(0))
faces = dataset.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=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.94, 0.04, 0.)
 
     
plot_gallery("First centered Olivetti faces", faces[:n_components])
###############################################################################
 
estimators = [
    ('Eigenfaces - PCA using randomized SVD',
         decomposition.PCA(n_components=9,whiten=True)),
 
    ('Non-negative components - NMF',
         decomposition.NMF(n_components=9, init='nndsvda', tol=5e-3))
]
 
###############################################################################
 
for name, estimator in estimators:
    print("Extracting the top %d %s..." % (n_components, name))
    print(faces.shape)
    estimator.fit(faces)
    components_ = estimator.components_
    plot_gallery(name, components_[:n_components])
 
plt.show()
image.png
image.png
image.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容