npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ml-som

v0.0.6

Published

Self-organizing map (SOM) / Kohonen network

Downloads

689

Readme

ml-som

self-organizing map (SOM) / Kohonen network

Installation

$ npm install ml-som

Methods

new SOM(x, y, [options])

Creates a new SOM instance with x * y dimensions.

Arguments

  • x - Dimension of the x axis
  • y - Dimension of the y axis
  • options - 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 be iterations * 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 randomly
  • traverse - Go sequentially through the training set
  • randomizer - 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 grid
  • hexa - Hexagonal grid
  • torus - 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(trainingSet)

Train the SOM with the provided trainingSet.

Arguments

  • trainingSet - Array of training elements. If the fields 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);

getConvertedNodes()

Returns a 2D array containing the nodes of the grid, in the structure described by the fields option.

setTraining(trainingSet)

Set the training set for use with the next method

trainOne()

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
}

predict([data], [computePosition])

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 (default: training set).
  • computePosition - True if you want to compute the position of the point in the cell, using the direct neighbors (default: false). This option is currently only implemented for rectangular grids.

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 }], true);
// result2 = [ [ 2, 26, [ 0.236, 0.694 ] ], [ 33, 12, [ 0.354, 0.152 ] ] ]

getFit([dataset])

Returns an array of fit values which are the square root of the distance between the input vector and its corresponding BMU.

Arguments

  • dataset - Array of vectors to for which to calculate fit values. Defaults to the training set.

getQuantizationError()

Returns the mean of the fit values for the training set. This number can be used to compare several runs of the same SOM.

export([includeDistance])

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).

SOM.load(model, [distanceFunction])

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 with som.export()
  • distanceFunction - Optionally provide the distance function used to create the model.

License

MIT