Kohonen Feature Map in N dimension (SOM)
Source code name: "neural_net_kohonen_mapND.py"
Programming language: Python
Topic: Artificial Intelligence/neural net
DMelt Version 1. Last modified: 05/09/2015. License: Pro
https://datamelt.org/code/cache/neural_net_kohonen_mapND_4306.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


# In Kohonen Feature Map, neurons are organizing themselves according to certain input values.
# wse 4x4 neutrons and plot the results as line. 
# (c) Chekanov

from jhpro.nnet.jknnl import *
from jhplot import *
from java.awt import *
import math
from java.util import Random


# create "matrics topology with 10 rows and 10 colums: 
topology = MatrixTopology(20,10)
#  topology = HexagonalTopology(5,10); 

# In our network all neurons will have only 2 inputs, 
# with random value. For that reason first we will define maximal value of neurons weigths 
maxWeight = (150,150)

# Now we can create network with specified topology, 2 inputs for each neuron and maximal value for neurons weights 
network = DefaultNetwork(2,maxWeight,topology)

# "function factor" - function which will be used to change neurons weights. 
# In this tutorial we will use "Constant Function Factor" with constant parameters set to 0.8. 
constantFactor = ConstantFunctionalFactor(0.2)

# make empty canvas in some range
c1 =HPlot("Canvas")
c1.visible()
c1.setLegend(0)
c1.setRange(0,100,0,150)
c1.setMarginLeft(70)
c1.setNameX("X")
c1.setNameY("Y")

pn= PND("data")
rand = Random()
inputSize=100   # number of random points in 2D 
for i in range(500):
      x=0.2*i+5*rand.nextGaussian()
      y=50+30*math.cos(0.2*x) + 10*rand.nextGaussian()
      pn.add([x,y])
      
c1.draw(pn.getP1D(0,1))

fileData = LearningData(pn)
learning = WTALearningFunction(network,200,EuclidesMetric(),fileData,constantFactor)
learning.learn()
# print network.toString()
out=network.getWeights()
print out.toString()

# show best neurons 
p2=P1D("Best")
p2.setColor(Color.red)
for i in range(pn.size()):
    best=learning.getBestNeuron(pn.get(i))
    weightList = network.getNeuron(best).getWeight()
    p2.add(weightList[0],weightList[1])
c1.draw(p2)


You see the box below because you did not login.