<jupyter_start><jupyter_text>In this example we will see how to use MiniSom to cluster the iris dataset.First, let's load the data and train our SOM:<jupyter_code>from minisom import MiniSom
import numpy as np
import pandas as pd

data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/00236/seeds_dataset.txt', 
                    names=['area', 'perimeter', 'compactness', 'length_kernel', 'width_kernel',
                   'asymmetry_coefficient', 'length_kernel_groove', 'target'], usecols=[0, 5], 
                   sep='\t+', engine='python')
# data normalization
data = (data - np.mean(data, axis=0)) / np.std(data, axis=0)
data = data.values

# Initialization and training
som_shape = (1, 3)
som = MiniSom(som_shape[0], som_shape[1], data.shape[1], sigma=.5, learning_rate=.5,
              neighborhood_function='gaussian', random_seed=10)

som.train_batch(data, 500, verbose=True)<jupyter_output>[   0 / 500 ]   0% - ? it/s
 [   0 / 500 ]   0% - ? it/s
 [   1 / 500 ]   0% - 0:00:00 left 
 [   2 / 500 ]   0% - 0:00:00 left 
 [   3 / 500 ]   1% - 0:00:00 left 
 [   4 / 500 ]   1% - 0:00:00 left 
 [   5 / 500 ]   1% - 0:00:00 left 
 [   6 / 500 ]   1% - 0:00:00 left 
 [   7 / 500 ]   1% - 0:00:00 left 
 [   8 / 500 ]   2% - 0:00:00 left 
 [   9 / 500 ]   2% - 0:00:00 left 
 [  10 / 500 ]   2% - 0:00:00 left 
 [  11 / 500 ]   2% - 0:00:00 left 
 [  12 / 500 ]   2% - 0:00:00 left 
 [  13 / 500 ]   3% - 0:00:00 left 
 [  14 / 500 ]   3% - 0:00:00 left 
 [  15 / 500 ]   3% - 0:00:00 left 
 [  16 / 500 ]   3% - 0:00:00 left 
 [  17 / 500 ]   3% - 0:00:00 left 
 [  18 / 500 ]   4% - 0:00:00 left 
 [  19 / 500 ]   4% - 0:00:00 left 
 [  20 / 500 ]   4% - 0:00:00 left 
 [  21 / 500 ]   4% - 0:00:00 left 
 [  22 / 500 ]   4% - 0:00:00 left 
 [  23 / 500 ]   5% - 0:00:00 left 
 [  24 / 500 ]   5% - 0:00:00 left 
 [  25 / 500 ]   5% - 0:00:00 left 
 [  26 / 500 ]   5% - 0:00:00 left 
 [  2[...]<jupyter_text>Now we will consider all the sample mapped into a specific neuron as a cluster. To identify each cluster more easily we will translate the bidimensional indexes of the neurons on the SOM into a monodimentional indexes:<jupyter_code># each neuron represents a cluster
winner_coordinates = np.array([som.winner(x) for x in data]).T
# with np.ravel_multi_index we convert the bidimensional
# coordinates to a monodimensional index
cluster_index = np.ravel_multi_index(winner_coordinates, som_shape)<jupyter_output><empty_output><jupyter_text>We can plot each cluster with a different color:<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline

# plotting the clusters using the first 2 dimentions of the data
for c in np.unique(cluster_index):
    plt.scatter(data[cluster_index == c, 0],
                data[cluster_index == c, 1], label='cluster='+str(c), alpha=.7)

# plotting centroids
for centroid in som.get_weights():
    plt.scatter(centroid[:, 0], centroid[:, 1], marker='x', 
                s=80, linewidths=35, color='k', label='centroid')
plt.legend();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14243614931237722, 'fraction_numerical': 0.10445317616240996, 'mean_word_length': 2.698547215496368, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '515079', 'license': 'MIT', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1120, 'n_words': 327, 'path': 'examples/Clustering.ipynb', 'repo_name': 'jriege555/minisom', 'size': '3054'}