Machine learning, C#, IA

Self-organizing map (SOM) implementation

I’ve always been fascinating by machine learning from data. I was reading about self-organizing map (SOM) and I wanted to try to implement a little program about it.

The program is based on the Kohonen maps, you create a NxM grid of neurons and trains it with a input data set. The learning process consists on find the similarities between input data and stored data from the map. After a while it will group the inputs with similar attributes together.


The program takes a CSV file as input based on numeric attributes and generate a graphical output. The workflow is quite simple as you can see in the following picture:

  • Generate a NxM matrix filled with random weights for each neuron ij
  • For each iteration:
    • Select a random input from the inputs set
    • Find the winner neuron (Best matching unit - BMU) that minimize the distance to the input
    • Update the weights for the winner and the neigbours neurons based on the ratio and influence parameter.
    • Update the learning ratio

Input data file

Data inputs come form a CSV separated by semicolon (;) where the first row is the description of the column attribute and each row is one input to the network. By default, it takes the last column as class attribute.

The following example is from the iris data set using in the video simulation:

Sepal length;Sepal width;Petal length;Petal width;Specie

User interface

  1. Input table: Values read from the CSV.
  2. Start button: Star the learning process. Once it start it will change for Stop.
  3. Save image: Save the actual image in PNG format.
  4. Progress bar: Shows the learning progress until reach the maximum number of iterations.
  5. Current execution time
  6. Variables to visualize
  7. Class variable: The classification variable.
  8. Number of iterations: Number of iterations used to learn the network.
  9. Numbers of rows and columns: Network dimension.
  10. Initial learning rate
  11. Lp constant: Used to compute the distance between neurons
  12. SOM Map Bidimensional representation of the map.
  13. Current cell info: Show the weight and the class of the current cell.
  14. Representation mode: Show inputs, distance matrix or neuron’s weights.
  15. Main menu

Here you can see the basic usage of the SOMVIS program:

  • Load a CSV dataset
  • Select the class variable. By default it will choose the last parameter.
  • Set the learning rate parameters
  • Click run and see how the SOM works

Learning example

The following video shows a SOM generated from the iris flower data set: