package com.imsl.test.example.datamining;
import com.imsl.datamining.*;
import com.imsl.stat.*;
/**
*
*
Creates and trains a Kohonen self-organizing
* map.
*
*
* This example creates a Kohonen network with 40 x 40 nodes using the class
* {@link KohonenSOM}. Each node has 3 weights, representing the RGB values of a
* color. This network is trained with 8 colors using 500 iterations. Then, the
* example prints out a forecast result.
*
*
* The initial image of the nodes is:
*
*
* After the training, the image is:
*
*
* @see Code
* @see Output
*
*/
public class KohonenSOMEx1 extends KohonenSOMTrainer {
private static int totalIter = 500, nrow = 40, ncol = 40;
private double initialLearning = 0.07;
@Override
public double getNeighborhoodValue(int t, double d) {
double factor, c;
// A Gaussian function.
factor = Math.max(nrow, ncol) / 4.0;
c = (double) (totalIter - t) / ((double) totalIter / factor);
return Math.exp(-(d * d) / (2.0 * c * c));
}
@Override
public double getLearningCoefficient(int t) {
return initialLearning * Math.exp(-(double) t / (double) totalIter);
}
public static void main(String args[]) {
double[][] data = {
{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}, {1.0, 1.0, 0.0},
{1.0, 0.0, 1.0}, {0.0, 1.0, 1.0},
{0.0, 0.0, 0.0}, {1.0, 1.0, 1.0}
};
// Use a Random object to set the weights.
Random rand = new Random(123457);
rand.setMultiplier(16807);
// Train the Kohonen network.
KohonenSOM kohonen = new KohonenSOM(3, nrow, ncol);
kohonen.setWeights(rand);
KohonenSOMEx1 trainer = new KohonenSOMEx1();
trainer.setIterations(totalIter);
trainer.train(kohonen, data);
// Get a forecast after training.
double[] fdata = {0.25, 0.50, 0.75};
int[] indices = kohonen.forecast(fdata);
System.out.printf("The input (%.2f, %.2f, %.2f) has forecasted "
+ "output (%d, %d) \nwhich corresponds to the pink area on "
+ "the estimated map.\n", fdata[0], fdata[1], fdata[2],
indices[0], indices[1]);
}
}