Example of a Kohonen map

In this example, we want to train a square 8 × 8 Kohonen map to become receptive to the Olivetti faces dataset. As each sample is a 64 × 64 grayscale image, we need to allocate a weight matrix with a shape equal to (8, 8, 4,096). The training process can be very long; therefore, we will limit the map to 100 random samples (of course, the reader is free to remove this limit and train the model with the whole dataset).

As usual, let's start by loading and normalizing the dataset, as follows:

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)X = Xcomplete[0:100]

Get Hands-On Unsupervised Learning with Python 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.