scikit-learn/examples/decomposition/plot_nmf.py

83 lines
2.4 KiB
Python
Raw Normal View History

2011-04-01 19:07:33 +08:00
"""
===================================================
NMF for digits feature extraction
2011-04-01 19:07:33 +08:00
===================================================
2011-04-03 20:57:04 +08:00
:ref:`NMF` with sparseness enforced in the components,
2011-04-02 03:10:40 +08:00
in comparison with PCA for feature extraction.
2011-04-01 19:07:33 +08:00
"""
print __doc__
from time import time
import logging
import pylab as pl
2011-04-04 19:31:48 +08:00
from scikits.learn.decomposition import RandomizedPCA, NMF
from scikits.learn import datasets
2011-04-01 19:07:33 +08:00
# Display progress logs on stdout
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
2011-04-02 03:04:15 +08:00
digits = datasets.load_digits()
2011-04-01 19:07:33 +08:00
# reshape the data using the traditional (n_samples, n_features) shape
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
2011-04-01 19:07:33 +08:00
n_features = X.shape[1]
n_components = 16
2011-04-01 19:07:33 +08:00
2011-04-02 03:10:40 +08:00
######################################################################
# Compute a PCA (eigendigits) on the digit dataset
print "Extracting the top %d eigendigits from %d images" % (
2011-04-01 19:07:33 +08:00
n_components, X.shape[0])
t0 = time()
pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X)
print "done in %0.3fs" % (time() - t0)
eigendigits = pca.components_
2011-04-01 19:07:33 +08:00
2011-04-02 03:04:15 +08:00
######################################################################
2011-04-02 03:10:40 +08:00
# Compute a NMF on the digit dataset
print "Extracting %d non-negative features from %d images" % (
2011-04-01 19:07:33 +08:00
n_components, X.shape[0])
t0 = time()
nmf = NMF(n_components=n_components, init='nndsvd', beta=5, tol=1e-2,
sparseness="components").fit(X)
2011-04-01 19:07:33 +08:00
print "done in %0.3fs" % (time() - t0)
nmfdigits = nmf.components_
2011-04-01 19:07:33 +08:00
2011-04-02 03:04:15 +08:00
######################################################################
2011-04-02 03:10:40 +08:00
# Plot the results
2011-04-01 19:07:33 +08:00
n_row, n_col = 4, 4
2011-04-01 19:07:33 +08:00
2011-04-03 07:32:27 +08:00
f1 = pl.figure(figsize=(1. * n_col, 1.13 * n_row))
2011-04-02 03:04:15 +08:00
f1.text(.5, .95, 'Principal components', horizontalalignment='center')
2011-04-01 19:07:33 +08:00
for i in range(n_row * n_col):
pl.subplot(n_row, n_col, i + 1)
pl.imshow(eigendigits[i].reshape((8, 8)), cmap=pl.cm.gray,
interpolation='nearest')
2011-04-01 19:07:33 +08:00
pl.xticks(())
pl.yticks(())
2011-04-03 07:32:27 +08:00
pl.subplots_adjust(0.01, 0.05, 0.99, 0.93, 0.04, 0.)
2011-04-01 19:07:33 +08:00
2011-04-03 07:32:27 +08:00
f2 = pl.figure(figsize=(1. * n_col, 1.13 * n_row))
2011-04-02 03:04:15 +08:00
f2.text(.5, .95, 'Non-negative components', horizontalalignment='center')
2011-04-01 19:07:33 +08:00
for i in range(n_row * n_col):
pl.subplot(n_row, n_col, i + 1)
pl.imshow(nmfdigits[i].reshape((8, 8)), cmap=pl.cm.gray,
interpolation='nearest')
2011-04-01 19:07:33 +08:00
pl.xticks(())
2011-04-02 03:04:15 +08:00
pl.yticks(())
2011-04-03 07:32:27 +08:00
pl.subplots_adjust(0.01, 0.05, 0.99, 0.93, 0.04, 0.)
2011-04-02 03:04:15 +08:00
pl.show()