SOM Example

This is a simple example of Self-Organizing Maps (SOM) using Python and the MiniSom library.

SOM Overview

Self-Organizing Maps (SOMs), also known as Kohonen maps, are neural network structures used for clustering and visualization of high-dimensional data. SOMs organize input data into a lower-dimensional grid of neurons, preserving the topological relationships between data points. They are particularly useful for exploring the structure of complex datasets and identifying patterns.

Key concepts of SOM:

SOMs have applications in data visualization, pattern recognition, and clustering.

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from minisom import MiniSom
from sklearn import datasets

# Load the Iris dataset
iris = datasets.load_iris()
data = iris.data
target = iris.target

# Normalize the data
data = (data - np.min(data, axis=0)) / (np.max(data, axis=0) - np.min(data, axis=0))

# Set SOM parameters
grid_size = (10, 10)
input_size = data.shape[1]
sigma = 1.0
learning_rate = 0.5

# Create and train the SOM
som = MiniSom(grid_size[0], grid_size[1], input_size, sigma=sigma, learning_rate=learning_rate)
som.random_weights_init(data)
som.train_random(data, 1000, verbose=True)

# Assign each data point to a cluster
cluster_labels = np.array([som.winner(x) for x in data])

# Plot the SOM clusters
plt.figure(figsize=(10, 8))
for i, (x, t) in enumerate(zip(data, target)):
    plt.text(cluster_labels[i][0], cluster_labels[i][1], str(t),
             color=plt.cm.rainbow(t / 3.), fontdict={'weight': 'bold', 'size': 9})
plt.scatter(cluster_labels[:, 0] + np.random.rand(len(data)) * 0.6 - 0.3,
            cluster_labels[:, 1] + np.random.rand(len(data)) * 0.6 - 0.3,
            c=target, cmap=plt.cm.rainbow, edgecolors='grey', s=50)
plt.title('SOM Clustering of Iris Dataset')
plt.show()

Explanation: