{"id":19266997,"url":"https://github.com/mljs/som","last_synced_at":"2025-04-21T19:32:26.931Z","repository":{"id":22245571,"uuid":"25579015","full_name":"mljs/som","owner":"mljs","description":"self-organizing map (SOM) / Kohonen network","archived":false,"fork":false,"pushed_at":"2016-11-23T21:23:59.000Z","size":31,"stargazers_count":23,"open_issues_count":0,"forks_count":4,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-01T15:41:46.817Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mljs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-22T11:58:24.000Z","updated_at":"2024-04-15T09:44:19.000Z","dependencies_parsed_at":"2022-08-21T00:30:53.020Z","dependency_job_id":null,"html_url":"https://github.com/mljs/som","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fsom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fsom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fsom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fsom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mljs","download_url":"https://codeload.github.com/mljs/som/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250120056,"owners_count":21378130,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T20:09:00.538Z","updated_at":"2025-04-21T19:32:26.925Z","avatar_url":"https://github.com/mljs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ml-som\n\n  [![NPM version][npm-image]][npm-url]\n  [![build status][travis-image]][travis-url]\n  [![David deps][david-image]][david-url]\n  [![npm download][download-image]][download-url]\n\nself-organizing map (SOM) / Kohonen network\n\n## Installation\n\n`$ npm install ml-som`\n\n## Methods\n\n### new SOM(x, y, [options])\n\nCreates a new SOM instance with x * y dimensions.\n\n__Arguments__\n\n* `x` - Dimension of the x axis\n* `y` - Dimension of the y axis\n* `options` - Object with options for the algorithm\n\n__Options__\n\n* `fields` - Either a number (size of input vectors) or a map of field descriptions (to convert them to vectors)\n* `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`\n* `learningRate` - Multiplication coefficient for the learning algorithm (default: 0.1)\n* `method` - Iteration method of the learning algorithm (default: random)\n *  `random` - Pick an object of the training set randomly\n *  `traverse` - Go sequentially through the training set\n* `randomizer` - Function that must give numbers between 0 and 1 (default: Math.random)\n* `distance` - Function that computes the distance between two vectors of the same length (default: squared Euclidean distance)\n* `gridType` - Shape of the grid (default: rect)\n * `rect` - Rectangular grid\n * `hexa` - Hexagonal grid\n* `torus` - Boolean indicating if the grid should be considered a torus for the selection of the neighbors (default: true)\n\n__Example__\n\n```js\nvar SOM = require('ml-som');\nvar options = {\n  fields: [\n    { name: 'r', range: [0, 255] },\n    { name: 'g', range: [0, 255] },\n    { name: 'b', range: [0, 255] }\n  ]\n};\n\nvar som = new SOM(20, 20, options);\n```\n\n### train(trainingSet)\n\nTrain the SOM with the provided `trainingSet`.\n\n__Arguments__\n\n* `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\n\n__Example__\n\n```js\nvar trainingSet = [\n  { r: 0, g: 0, b: 0 },\n  { r: 255, g: 0, b: 0 },\n  { r: 0, g: 255, b: 0 },\n  { r: 0, g: 0, b: 255 },\n  { r: 255, g: 255, b: 255 }\n];\n\nsom.train(trainingSet);\n```\n\n### getConvertedNodes()\n\nReturns a 2D array containing the nodes of the grid, in the structure described by the `fields` option.\n\n### setTraining(trainingSet)\n\nSet the training set for use with the next method\n\n### trainOne()\n\nExecutes 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.\n\n__Example__\n\n```js\nsom.setTraining(trainingSet);\nwhile(som.trainOne()) {\n  var nodes = som.getConvertedNodes();\n  // do something with the nodes\n}\n```\n\n### predict([data], [computePosition])\n\nReturns for each data point the coordinates of the corresponding best matching unit (BMU) on the grid\n\n__Arguments__\n\n* `data` - Data point or array of data points (default: training set).\n* `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.\n\n__Example__\n\n```js\n// create and train the som\nvar result1 = som.predict({ r: 45, g: 209, b: 100 });\n// result1 = [ 2, 26 ]\nvar result2 = som.predict([{ r: 45, g: 209, b: 100 }, { r: 155, g: 22, b: 12 }], true);\n// result2 = [ [ 2, 26, [ 0.236, 0.694 ] ], [ 33, 12, [ 0.354, 0.152 ] ] ]\n```\n\n### getFit([dataset])\n\nReturns an array of fit values which are the square root of the distance between the input vector and its corresponding BMU.\n\n__Arguments__\n\n* `dataset` - Array of vectors to for which to calculate fit values. Defaults to the training set.\n\n### getQuantizationError()\n\nReturns the mean of the fit values for the training set. This number can be used to compare several runs of the same SOM.\n\n### getUMatrix()\n\nReturns a 2D array representing the grid. Each value is the mean of the distances between the corresponding node and its direct neighbors. Currently only available for square nodes\n\n### export()\n\nExports the model to a JSON object that can be written to disk and reloaded\n\n### SOM.load(model, [distanceFunction])\n\nReturns 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.\n\n__Arguments__\n\n* `model` - JSON object generated with `som.export()`\n* `distanceFunction` - Optionally provide the distance function used to create the model.\n\n## License\n\n  [MIT](./LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/ml-som.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/ml-som\n[travis-image]: https://img.shields.io/travis/mljs/som/master.svg?style=flat-square\n[travis-url]: https://travis-ci.org/mljs/som\n[david-image]: https://img.shields.io/david/mljs/som.svg?style=flat-square\n[david-url]: https://david-dm.org/mljs/som\n[download-image]: https://img.shields.io/npm/dm/ml-som.svg?style=flat-square\n[download-url]: https://npmjs.org/package/ml-som\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fsom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmljs%2Fsom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fsom/lists"}