Example of SOM

We can now implement an SOM using the Olivetti faces dataset. As the process can be very long, in this example we limit the number of input patterns to 100 (with a 5 × 5 matrix). The reader can try with the whole dataset and a larger map.

The first step is loading the data, normalizing it so that all values are bounded between 0.0 and 1.0, and setting the constants:

import numpy as npfrom sklearn.datasets import fetch_olivetti_facesfaces = fetch_olivetti_faces(shuffle=True)Xcomplete = faces['data'].astype(np.float64) / np.max(faces['data'])np.random.shuffle(Xcomplete)nb_iterations = 5000nb_startup_iterations = 500pattern_length = 64 * 64pattern_width = pattern_height = 64eta0 = 1.0sigma0 = 3.0tau = 100.0X = Xcomplete[0:100] ...

Get Python: Advanced Guide to Artificial Intelligence now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.