日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

矩阵分解系列三:非负矩阵分解及Python实现

發布時間:2024/8/26 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 矩阵分解系列三:非负矩阵分解及Python实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

非負矩陣分解的定義及理解

「摘自《遷移學習》K-Means算法&非負矩陣三因子分解(NMTF)」

下圖可幫助理解:

舉個簡單的人臉重構例子:

Python實例:用非負矩陣分解提取人臉特征

「摘自Python機器學習應用」

在sklearn庫中,可以使用sklearn.decomposition.NMF加載NMF算法,主要參數有:

n_components:指定分解后基向量矩陣W的基向量個數k
init:W矩陣和Z矩陣的初始化方式,默認為‘nndsvdar’

目標:已知Olivetti人臉數據共400個,每個數據是64*64大小。由于NMF分解得到的W矩陣相當于從原始矩陣中提取的特征,那么就可以使用NMF對400個人臉數據進行特征提取。
程序如下:

from numpy.random import RandomState
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_olivetti_faces   #加載Olivetti人臉數據集導入函數
from sklearn import decomposition

n_row, n_col = 2, 3
n_components = n_row * n_col   #設置提取的特征的數目
image_shape = (64, 64)   #設置展示時人臉數據圖片的大小

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=n_components, whiten=True)),
              ('Non-negative components - NMF', decomposition.NMF(n_components=n_components, 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)   #調用PCA或NMF提取特征
    components_ = estimator.components_   #獲取提取的特征
    plot_gallery(name, components_[:n_components])

plt.show()  

運行結果:

Extracting the top 6 Eigenfaces - PCA using randomized SVD...
(400, 4096)
Extracting the top 6 Non-negative components - NMF...
(400, 4096)


總結

以上是生活随笔為你收集整理的矩阵分解系列三:非负矩阵分解及Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。