NMF 方法及其实例

非负矩阵分解(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])

输出:

这里写图片描述

这里写图片描述

这里写图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值