如何从包含子图

时间:2018-05-10 14:23:07

标签: python animation matplotlib visualization

我正在使用MNIST数据库上的非负矩阵因子分解(NMF)算法,我希望在减少组件总数时可视化前15个组件的变化。

首先,我下载MNIST数据库并将其转换为numPy数组

import numpy as np
from mnist import MNIST
mndata = MNIST('./data')

images_train, labels_train = mndata.load_training()
images_test, labels_test = mndata.load_testing()

labels_train = labels_train.tolist()
labels_test = labels_test.tolist()

X_train = np.array(images_train).astype('float64')
y_train = np.array(labels_train)
X_test = np.array(images_test).astype('float64')
y_test = np.array(labels_test)

然后我使用100组件构建NMF模型

from  sklearn.decomposition import NMF

nmf = NMF(n_components=100, random_state=0)
nmf.fit(X_train)

我显示前15个组件

import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 5, figsize=(12, 7), subplot_kw={'xticks': (), 'yticks': ()})
for i, (component, ax) in enumerate(zip(nmf.components_, axes.ravel())):
    ax.imshow(component.reshape(28,28), cmap=plt.cm.binary)
    ax.set_title("{}. component".format(i + 1))

输出:enter image description here

然后我为50组件重复相同的过程

输出:enter image description here

,最后是15个组件

输出:enter image description here

我想知道当我减少总组件数量时,这些NMF模型的前15个组件是否会顺利改变。我想绘制像上面这些图片的大量图片,并在动画中显示它们。更确切地说,当总组件数从100增加到15并且步长等于5时,我想为它们设置动画。我怎么能这样做?

0 个答案:

没有答案