"""
Online learning of a dictionary of parts of faces
==================================================

This example uses a large dataset of faces to learn a set of 20 x 20
images patches that constitute faces.

From the programming standpoint, it is interesting because it shows how
to use the online API of the scikit-learn to process a very large
dataset by chunks. The way we proceed is that we load an image at a time
and extract randomly 15 patches from this image. Once we have accumulated
750 of these patches (using 50 images), we run the `partial_fit` method
of the online KMeans object, MiniBatchKMeans.
"""
print __doc__

import time

import pylab as pl
import numpy as np


from sklearn import datasets
from sklearn.cluster import MiniBatchKMeans
from sklearn.feature_extraction.image import extract_patches_2d

faces = datasets.fetch_lfw_people()
data = faces.data

###############################################################################
# Learn the dictionary of images

print 'Learning the dictionary... '
rng = np.random.RandomState(0)
kmeans = MiniBatchKMeans(n_clusters=81)
patch_size = (20, 20)

buffer = []
index = 1
t0 = time.time()

# The online learning part
for index, img in enumerate(faces.images):
    data = extract_patches_2d(img, patch_size,
                                max_patches=15, random_state=rng)
    data = np.reshape(data, (len(data), -1))
    buffer.append(data)
    index += 1
    if index % 50 == 0:
        data = np.concatenate(buffer, axis=0)
        data -= np.mean(data, axis=0)
        data /= np.std(data, axis=0)
        kmeans.partial_fit(data)
        buffer = []
    if index % 500 == 0:
        print 'Partial fit of %4i out of %i' % (index,
                                                len(faces.images))

dt = time.time() - t0
print 'done in %.2fs.' % dt

###############################################################################
# Plot the results
pl.figure(figsize=(4.2, 4))
for i, patch in enumerate(kmeans.cluster_centers_):
    pl.subplot(9, 9, i + 1)
    pl.imshow(patch.reshape(patch_size), cmap=pl.cm.gray,
              interpolation='nearest')
    pl.xticks(())
    pl.yticks(())

pl.suptitle('Patches of faces\nTrain time %.1fs on %d patches' %
            (dt, len(faces.images)), fontsize=16)
pl.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)

pl.show()

@@ -609,16 +609,283 @@
 hKMeans.
+%0A%0AThe verbose setting on the MiniBatchKMeans enables us to see that some%0Aclusters are reassigned during the successive calls to%0Apartial-fit. This is because the number of patches that they represent%0Ahas become too low, and it is better to choose a random new%0Acluster.
 %0A%22%22%22%0Apri
@@ -1114,38 +1114,24 @@
 tch_
-lfw_people()%0Adata = faces.data
+olivetti_faces()
 %0A%0A##
@@ -1347,16 +1347,48 @@
 sters=81
+, random_state=rng, verbose=True
 )%0Apatch_
@@ -1474,37 +1474,91 @@
 part
-%0Afor index, img in enumerate(
+: cycle over the whole dataset 4 times%0Aindex = 0%0Afor _ in range(6):%0A    for img in 
 face
@@ -1565,19 +1565,22 @@
 s.images
-)
 :%0A
+    
     data
@@ -1650,16 +1650,20 @@
         
+    
 max_patc
@@ -1666,18 +1666,18 @@
 patches=
-1
 5
+0
 , random
@@ -1688,24 +1688,28 @@
 te=rng)%0A    
+ 
+   
 data = np.re
@@ -1737,16 +1737,20 @@
 ), -1))%0A
+    
     buff
@@ -1761,24 +1761,28 @@
 ppend(data)%0A
+    
     index +=
@@ -1784,16 +1784,20 @@
 ex += 1%0A
+    
     if i
@@ -1807,17 +1807,17 @@
 x %25 
-5
+1
 0 == 0:%0A
     
@@ -1804,32 +1804,36 @@
 ndex %25 10 == 0:%0A
+    
         data = n
@@ -1866,24 +1866,28 @@
 =0)%0A        
+ 
+   
 data -= np.m
@@ -1908,24 +1908,28 @@
 =0)%0A        
+   
+ 
 data /= np.s
@@ -1937,32 +1937,36 @@
 d(data, axis=0)%0A
+    
         kmeans.p
@@ -1986,24 +1986,28 @@
 ta)%0A        
+    
 buffer = %5B%5D%0A
@@ -2006,24 +2006,28 @@
 er = %5B%5D%0A    
+   
+ 
 if index %25 5
@@ -2025,17 +2025,17 @@
 index %25 
-5
+1
 00 == 0:
@@ -2027,32 +2027,36 @@
 dex %25 100 == 0:%0A
+    
         print 'P
@@ -2141,16 +2141,24 @@
         
+     6 *
  len(fac
@@ -2562,16 +2562,17 @@
 ks(())%0A%0A
+%0A
 pl.supti
@@ -2644,16 +2644,20 @@
     (dt,
+ 8 *
  len(fac

change the example to lighter dataset