楼主: ReneeBK
1283 2

Self-organizing Map using JavaScript [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4898份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49640 个
通用积分
55.8137
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2016-6-27 00:12:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
ml-som

self-organizing map (SOM) / Kohonen network

Installation

$ npm install ml-som

Methodsnew 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: squared 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 somvar 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.

getUMatrix()

Returns 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

export()

Exports the model to a JSON object that can be written to disk and reloaded

SOM.load(model, [distanceFunction])

Returns a new SOM instance based on the model. If the model was created with a custom distance function, the distanceargument 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


本帖隐藏的内容

som-master.zip (10.19 KB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Javascript Organizing script scrip Using default training convert Object number

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-6-27 00:13:03
  1. 'use strict';

  2. var NodeSquare = require('./node-square');

  3. function NodeHexagonal(x, y, weights, som) {

  4.     NodeSquare.call(this, x, y, weights, som);

  5.     this.hX = x - Math.floor(y / 2);
  6.     this.z = 0 - this.hX - y;

  7. }

  8. NodeHexagonal.prototype = new NodeSquare();
  9. NodeHexagonal.prototype.constructor = NodeHexagonal;

  10. NodeHexagonal.prototype.getDistance = function getDistanceHexagonal(otherNode) {
  11.     return Math.max(Math.abs(this.hX - otherNode.hX), Math.abs(this.y - otherNode.y), Math.abs(this.z - otherNode.z));
  12. };

  13. NodeHexagonal.prototype.getDistanceTorus = function getDistanceTorus(otherNode) {
  14.     var distX = Math.abs(this.hX - otherNode.hX),
  15.         distY = Math.abs(this.y - otherNode.y),
  16.         distZ = Math.abs(this.z - otherNode.z);
  17.     return Math.max(Math.min(distX, this.som.gridDim.x - distX), Math.min(distY, this.som.gridDim.y - distY), Math.min(distZ, this.som.gridDim.z - distZ));
  18. };

  19. NodeHexagonal.prototype.getPosition = function getPosition() {
  20.     throw new Error('Unimplemented : cannot get position of the points for hexagonal grid');
  21. };

  22. module.exports = NodeHexagonal;
复制代码

藤椅
ReneeBK 发表于 2016-6-27 00:13:56
  1. 'use strict';

  2. function NodeSquare(x, y, weights, som) {
  3.     this.x = x;
  4.     this.y = y;
  5.     this.weights = weights;
  6.     this.som = som;
  7.     this.neighbors = {};
  8. }

  9. NodeSquare.prototype.adjustWeights = function adjustWeights(target, learningRate, influence) {
  10.     for (var i = 0, ii = this.weights.length; i < ii; i++) {
  11.         this.weights[i] += learningRate * influence * (target[i] - this.weights[i]);
  12.     }
  13. };

  14. NodeSquare.prototype.getDistance = function getDistance(otherNode) {
  15.     return Math.max(Math.abs(this.x - otherNode.x), Math.abs(this.y - otherNode.y));
  16. };

  17. NodeSquare.prototype.getDistanceTorus = function getDistanceTorus(otherNode) {
  18.     var distX = Math.abs(this.x - otherNode.x),
  19.         distY = Math.abs(this.y - otherNode.y);
  20.     return Math.max(Math.min(distX, this.som.gridDim.x - distX), Math.min(distY, this.som.gridDim.y - distY));
  21. };

  22. NodeSquare.prototype.getNeighbors = function getNeighbors(xy) {
  23.     if (!this.neighbors[xy]) {
  24.         this.neighbors[xy] = new Array(2);

  25.         // left or bottom neighbor
  26.         var v;
  27.         if (this[xy] > 0) {
  28.             v = this[xy] - 1;
  29.         } else if (this.som.torus) {
  30.             v = this.som.gridDim[xy] - 1;
  31.         }
  32.         if (typeof v !== 'undefined') {
  33.             var x, y;
  34.             if (xy === 'x') {
  35.                 x = v;
  36.                 y = this.y;
  37.             } else {
  38.                 x = this.x;
  39.                 y = v;
  40.             }
  41.             this.neighbors[xy][0] = this.som.nodes[x][y];
  42.         }

  43.         // top or right neighbor
  44.         var w;
  45.         if (this[xy] < (this.som.gridDim[xy] - 1)) {
  46.             w = this[xy] + 1;
  47.         } else if (this.som.torus) {
  48.             w = 0;
  49.         }
  50.         if (typeof w !== 'undefined') {
  51.             if (xy === 'x') {
  52.                 x = w;
  53.                 y = this.y;
  54.             } else {
  55.                 x = this.x;
  56.                 y = w;
  57.             }
  58.             this.neighbors[xy][1] = this.som.nodes[x][y];
  59.         }
  60.     }
  61.     return this.neighbors[xy];
  62. };

  63. NodeSquare.prototype.getPos = function getPos(xy, element) {
  64.     var neighbors = this.getNeighbors(xy),
  65.         distance = this.som.distance,
  66.         bestNeighbor,
  67.         direction;
  68.     if(neighbors[0]) {
  69.         if (neighbors[1]) {
  70.             var dist1 = distance(element, neighbors[0].weights),
  71.                 dist2 = distance(element, neighbors[1].weights);
  72.             if(dist1 < dist2) {
  73.                 bestNeighbor = neighbors[0];
  74.                 direction = -1;
  75.             } else {
  76.                 bestNeighbor = neighbors[1];
  77.                 direction = 1;
  78.             }
  79.         } else {
  80.             bestNeighbor = neighbors[0];
  81.             direction = -1;
  82.         }
  83.     } else {
  84.         bestNeighbor = neighbors[1];
  85.         direction = 1;
  86.     }
  87.     var simA = 1 - distance(element, this.weights),
  88.         simB = 1 - distance(element, bestNeighbor.weights);
  89.     var factor = ((simA - simB) / (2 - simA - simB));
  90.     return 0.5 + 0.5 * factor * direction;
  91. };

  92. NodeSquare.prototype.getPosition = function getPosition(element) {
  93.     return [
  94.         this.getPos('x', element),
  95.         this.getPos('y', element)
  96.     ];
  97. };

  98. module.exports = NodeSquare;
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-8 13:00