self-organizing map (SOM) / Kohonen network
$ npm install ml-som
Creates a new SOM instance with x * y dimensions.
Arguments
x
- Dimension of the x axisy
- Dimension of the y axisoptions
- Object with options for the algorithm
Options
fields
- Either a number (size of input vectors) or a map of field descriptions (to convert them to vectors)iterations
- Number of iterations over the training set for the training phase (default: 10). The total number of training steps will beiterations
*trainingSet.length
learningRate
- Multiplication coefficient for the learning algorithm (default: 0.1)method
- Iteration method of the learning algorithm (default: random)random
- Pick an object of the training set randomlytraverse
- Go sequentially through the training setrandomizer
- Function that must give numbers between 0 and 1 (default: Math.random)distance
- Function that computes the distance between two vectors of the same length (default: square euclidean distance)gridType
- Shape of the grid (default: rect)rect
- Rectangular gridhexa
- Hexagonal gridtorus
- Boolean indicating if the grid should be considered a torus for the selection of the neighbors (default: true)
Example
var SOM = require('ml-som');
var options = {
fields: {
r: [0, 255],
g: [0, 255],
b: [0, 255]
}
};
var som = new SOM(20, 20, options);
Train the SOM with the provided trainingSet
.
Arguments
trainingSet
- Array of training elements. If thefields
was a number, each array element must be a normalized vector. If it was an object, each array element must be an object with at least the described properties, within the described ranges
Example
var trainingSet = [
{ r: 0, g: 0, b: 0 },
{ r: 255, g: 0, b: 0 },
{ r: 0, g: 255, b: 0 },
{ r: 0, g: 0, b: 255 },
{ r: 255, g: 255, b: 255 }
];
som.train(trainingSet);
Returns a 2D array containing the nodes of the grid, in the structure described by the fields
option.
Set the training set for use with the next method
Executes the next training iteration and returns true. Returns false if the training is over. Useful to draw the grid or compute some things after each learning step.
Example
som.setTraining(trainingSet);
while(som.trainOne()) {
var nodes = som.getConvertedNodes();
// do something with the nodes
}
Returns for each data point the coordinates of the corresponding best matching unit (BMU) on the grid
Arguments
data
- Data point or array of data points.
Example
// create and train the som
var result1 = som.predict({ r: 45, g: 209, b: 100 });
// result1 = [ 2, 26 ]
var result2 = som.predict([{ r: 45, g: 209, b: 100 }, { r: 155, g: 22, b: 12 }]);
// result2 = [ [ 2, 26 ], [ 33, 12 ] ]
Returns the mean of the euclidean distance between each point of the training set and its corresponding BMU. This number can be used to compare several runs of the same SOM.
Exports the model to a JSON object that can be written to disk and reloaded
Arguments
includeDistance
- Boolean indicating if the distance function should be included in the model as a String (not recommended). Note that there is no need to include the default function and that it cannot work if the function depends on variables that are out of its scope (default: false).
Returns a new SOM instance based on the model
. If the model was created with a custom distance function, the distance
argument should be this function.
Arguments
model
- JSON object generated withsom.export()
distanceFunction
- Optionally provide the distance function used to create the model.
MIT