机器学习 sklearn 无监督学习 降维算法 NMF Non-negative Matrix Factorization

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets import make_blobs

from sklearn.decomposition import NMF
from sklearn.datasets import load_iris

iris = load_iris()
iris_X = iris.data   #x有4个属性,共有150个样本点
iris_y = iris.target #y的取值有3个,分别是0,1,2

NMF = NMF(n_components=2)
NMF.fit(iris_X)

X_new = NMF.transform(iris_X)

# plt.scatter(X_new[:], X_new[:], marker='o',c=iris_y)

plt.scatter(X_new[:, 0], X_new[:, 1], marker='o',c=iris_y)

# fig = plt.figure()
# ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=30, azim=20)
# plt.scatter(X_new[:, 0], X_new[:, 1], X_new[:, 2], marker='o',c=iris_y)

plt.show()

降为一维

在这里插入图片描述

降为二维

在这里插入图片描述
(不指定维度)
在这里插入图片描述

降为三维

在这里插入图片描述

应用

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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))] #NMF和PCA实例化

for name,estimator in estimators: #分别调用PCA和NMF
    estimator.fit(faces) #调用PCA或NMF提取特征
    components_=estimator.components_ #获取提取的特征
    plot_gallery(name,components_[:n_components]) #按照固定格式进行排列
plt.show()

效果展示

在这里插入图片描述

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值