<jupyter_start><jupyter_text>This example applies to The [Olivetti faces dataset](http://scikit-learn.org/stable/datasets/olivetti_faces.htmlolivetti-faces) different unsupervised matrix decomposition (dimension reduction) methods from the module [sklearn.decomposition](http://scikit-learn.org/stable/modules/classes.htmlmodule-sklearn.decomposition) ([see the documentation chapter Decomposing signals in components (matrix factorization problems)](http://scikit-learn.org/stable/modules/decomposition.htmldecompositions)) .  New to Plotly?Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).You can set up Plotly to work in [online](https://plot.ly/python/getting-started/initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/start-plotting-online).We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!  Version<jupyter_code>import sklearn
sklearn.__version__<jupyter_output><empty_output><jupyter_text>Imports This tutorial imports [fetch_olivetti_faces](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.fetch_olivetti_faces.htmlsklearn.datasets.fetch_olivetti_faces) and [MiniBatchKMeans](http://scikit-learn.org/stable/modules/generated/sklearn.cluster.MiniBatchKMeans.htmlsklearn.cluster.MiniBatchKMeans).<jupyter_code>print(__doc__)

import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import logging
from time import time
from numpy.random import RandomState
import matplotlib.pyplot as plt
import numpy as np


from sklearn.datasets import fetch_olivetti_faces
from sklearn.cluster import MiniBatchKMeans
from sklearn import decomposition<jupyter_output>Automatically created module for IPython interactive environment<jupyter_text>Calculations<jupyter_code># Display progress logs on stdout
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s')
n_row, n_col = 2, 3
n_components = n_row * n_col
image_shape = (64, 64)
rng = RandomState(0)<jupyter_output><empty_output><jupyter_text>Load faces data<jupyter_code>dataset = fetch_olivetti_faces(shuffle=True, random_state=rng)
faces = dataset.data

n_samples, n_features = faces.shape

# global centering
faces_centered = faces - faces.mean(axis=0)

# local centering
faces_centered -= faces_centered.mean(axis=1).reshape(n_samples, -1)

print("Dataset consists of %d faces" % n_samples)<jupyter_output>Dataset consists of 400 faces<jupyter_text>List of the different estimators, whether to center and transpose the problem, and whether the transformer uses the clustering API.<jupyter_code>estimators = [
    ('Eigenfaces - PCA using randomized SVD',
     decomposition.PCA(n_components=n_components, svd_solver='randomized',
                       whiten=True),
     True),

    ('Non-negative components - NMF',
     decomposition.NMF(n_components=n_components, init='nndsvda', tol=5e-3),
     False),

    ('Independent components - FastICA',
     decomposition.FastICA(n_components=n_components, whiten=True),
     True),

    ('Sparse comp. - MiniBatchSparsePCA',
     decomposition.MiniBatchSparsePCA(n_components=n_components, alpha=0.8,
                                      n_iter=100, batch_size=3,
                                      random_state=rng),
     True),

    ('MiniBatchDictionaryLearning',
        decomposition.MiniBatchDictionaryLearning(n_components=15, alpha=0.1,
                                                  n_iter=50, batch_size=3,
                                                  random_state=rng),
     True),

    ('Cluster centers - MiniBatchKMeans',
        MiniBatchKMeans(n_clusters=n_components, tol=1e-3, batch_size=20,
                        max_iter=50, random_state=rng),
     True),

    ('Factor Analysis components - FA',
     decomposition.FactorAnalysis(n_components=n_components, max_iter=2),
     True),
]<jupyter_output><empty_output><jupyter_text>Plot Results<jupyter_code>def matplotlib_to_plotly(cmap, pl_entries):
    h = 1.0/(pl_entries-1)
    pl_colorscale = []
    
    for k in range(pl_entries):
        C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
        pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
        
    return pl_colorscale
def plot_gallery(title, images, n_col=n_col, n_row=n_row):
    fig = tools.make_subplots(rows=n_row, cols=n_col,
                              print_grid=False)
    
    for i, comp in enumerate(images):
        vmax = max(comp.max(), -comp.min())
        trace = go.Heatmap(z=comp.reshape(image_shape),
                           colorscale=matplotlib_to_plotly(plt.cm.gray, 20),
                           showscale=False
                          )
        if(i<3):
            row = 1
        else:
            row = 2 
            
        fig.append_trace(trace, row, i%3 +1)
    
    for i in map(str,range(1, (n_col*n_row) + 1)):
        y = 'yaxis'+ i
        x = 'xaxis'+i
        fig['layout'][y].update(autorange='reversed',
                                   showticklabels=False, ticks='')
        fig['layout'][x].update(showticklabels=False, ticks='')
        
    fig['layout'].update(title=title)
    return fig<jupyter_output><empty_output><jupyter_text>First centered Olivetti faces<jupyter_code>py.iplot(plot_gallery("First centered Olivetti faces", faces_centered[:n_components]))<jupyter_output><empty_output><jupyter_text>Do the estimation and plot it<jupyter_code>plot = []
for name, estimator, center in estimators:
    print("Extracting the top %d %s..." % (n_components, name))
    t0 = time()
    data = faces
    if center:
        data = faces_centered
    estimator.fit(data)
    train_time = (time() - t0)
    print("done in %0.3fs" % train_time)
    if hasattr(estimator, 'cluster_centers_'):
        components_ = estimator.cluster_centers_
    else:
        components_ = estimator.components_
    if (hasattr(estimator, 'noise_variance_') and
            estimator.noise_variance_.shape != ()):
        plot.append(plot_gallery("Pixelwise variance",
                                 estimator.noise_variance_.reshape(1, -1), n_col=1,
                                 n_row=1))
        
    plot.append(plot_gallery('%s - Train time %.1fs' % (name, train_time),
                 components_[:n_components]))<jupyter_output>Extracting the top 6 Eigenfaces - PCA using randomized SVD...
done in 0.298s
Extracting the top 6 Non-negative components - NMF...
done in 0.776s
Extracting the top 6 Independent components - FastICA...
done in 0.784s
Extracting the top 6 Sparse comp. - MiniBatchSparsePCA...
done in 0.947s
Extracting the top 6 MiniBatchDictionaryLearning...
done in 1.884s
Extracting the top 6 Cluster centers - MiniBatchKMeans...
done in 0.112s
Extracting the top 6 Factor Analysis components - FA...
done in 0.164s<jupyter_text>Eigenfaces PCA using randomized SVD<jupyter_code>py.iplot(plot[0])<jupyter_output><empty_output><jupyter_text>Non-negative components - NMF<jupyter_code>py.iplot(plot[1])<jupyter_output><empty_output><jupyter_text>Independent components - FastlCA<jupyter_code>py.iplot(plot[2])<jupyter_output><empty_output><jupyter_text>Sparse comp. - MiniBatchSparsePCA<jupyter_code>py.iplot(plot[3])<jupyter_output><empty_output><jupyter_text>MiniBatchDictionaryLearning<jupyter_code>py.iplot(plot[4])<jupyter_output><empty_output><jupyter_text>Cluster Centers - MiniBatchKmeans<jupyter_code>py.iplot(plot[5])<jupyter_output><empty_output><jupyter_text>Pixelwise Variance<jupyter_code>py.iplot(plot[6])<jupyter_output><empty_output><jupyter_text>Factor Analysis Components<jupyter_code>py.iplot(plot[7])<jupyter_output><empty_output><jupyter_text>License Authors:           <NAME>                     <NAME>          License:          BSD 3 clause<jupyter_code>from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'faces-dataset-decompositions.ipynb', 'scikit-learn/plot-faces-decomposition/', 'Faces Dataset Decompositions | plotly',
    ' ',
    title = 'Faces Dataset Decompositions | plotly',
    name = 'Faces Dataset Decompositions',
    has_thumbnail='true', thumbnail='thumbnail/fc.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='decomposition', order=9,
    ipynb= '~Diksha_Gabha/2961')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11596301659797259, 'fraction_numerical': 0.014592848390330846, 'mean_word_length': 3.8873162765378333, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 70, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '554614', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 3051, 'n_tokens_neox': 2885, 'n_words': 528, 'path': '_posts/scikit/faces-dataset-decompositions/faces-dataset-decompositions.ipynb', 'repo_name': 'bmb804/documentation', 'size': '8995'}