11/**
22 * ml - Machine learning tools
3- * @version v0.1.2
3+ * @version v0.1.3
44 * @link https://github.com/mljs/ml
55 * @license MIT
66 */
@@ -3765,130 +3765,6 @@ Matrix.MatrixError = MatrixError;
37653765
37663766module . exports = Matrix ;
37673767} , { } ] , 56 :[ function ( require , module , exports ) {
3768- module . exports = require ( './som' ) ;
3769- } , { "./som" :59 } ] , 57 :[ function ( require , module , exports ) {
3770- var NodeSquare = require ( './node-square' ) ;
3771-
3772- function NodeHexagonal ( x , y , weights , som ) {
3773-
3774- NodeSquare . call ( this , x , y , weights , som ) ;
3775-
3776- this . hX = x - Math . floor ( y / 2 ) ;
3777- this . z = 0 - this . hX - y ;
3778-
3779- }
3780-
3781- NodeHexagonal . prototype = new NodeSquare ;
3782- NodeHexagonal . prototype . constructor = NodeHexagonal ;
3783-
3784- NodeHexagonal . prototype . getDistance = function getDistanceHexagonal ( otherNode ) {
3785- return Math . max ( Math . abs ( this . hX - otherNode . hX ) , Math . abs ( this . y - otherNode . y ) , Math . abs ( this . z - otherNode . z ) ) ;
3786- } ;
3787-
3788- NodeHexagonal . prototype . getDistanceTorus = function getDistanceTorus ( otherNode ) {
3789- var distX = Math . abs ( this . hX - otherNode . hX ) ,
3790- distY = Math . abs ( this . y - otherNode . y ) ,
3791- distZ = Math . abs ( this . z - otherNode . z ) ;
3792- 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 ) ) ;
3793- } ;
3794-
3795- NodeHexagonal . prototype . getPosition = function getPosition ( ) {
3796- throw new Error ( 'Unimplemented : cannot get position of the points for hexagonal grid' ) ;
3797- } ;
3798-
3799- module . exports = NodeHexagonal ;
3800- } , { "./node-square" :58 } ] , 58 :[ function ( require , module , exports ) {
3801- function NodeSquare ( x , y , weights , som ) {
3802- this . x = x ;
3803- this . y = y ;
3804- this . weights = weights ;
3805- this . som = som ;
3806- }
3807-
3808- NodeSquare . prototype . adjustWeights = function adjustWeights ( target , learningRate , influence ) {
3809- for ( var i = 0 , ii = this . weights . length ; i < ii ; i ++ ) {
3810- this . weights [ i ] += learningRate * influence * ( target [ i ] - this . weights [ i ] ) ;
3811- }
3812- } ;
3813-
3814- NodeSquare . prototype . getDistance = function getDistance ( otherNode ) {
3815- return Math . max ( Math . abs ( this . x - otherNode . x ) , Math . abs ( this . y - otherNode . y ) ) ;
3816- } ;
3817-
3818- NodeSquare . prototype . getDistanceTorus = function getDistanceTorus ( otherNode ) {
3819- var distX = Math . abs ( this . x - otherNode . x ) ,
3820- distY = Math . abs ( this . y - otherNode . y ) ;
3821- return Math . max ( Math . min ( distX , this . som . gridDim . x - distX ) , Math . min ( distY , this . som . gridDim . y - distY ) ) ;
3822- } ;
3823-
3824- NodeSquare . prototype . getPosition = function getPosition ( element ) {
3825- var neighbors = this . neighbors || this . getNeighbors ( ) ;
3826- var neighbor , dist ;
3827- var position = [ 0.5 , 0.5 ] ;
3828- for ( var i = 0 ; i < neighbors . length ; i ++ ) {
3829- neighbor = neighbors [ i ] ;
3830- dist = 1 - this . som . distance ( neighbor . weights , element ) / this . som . maxDistance ;
3831- var xFactor = ( neighbor . x - this . x ) ;
3832- if ( xFactor > 1 ) {
3833- xFactor = - 1 ;
3834- } else if ( xFactor < - 1 ) {
3835- xFactor = 1 ;
3836- }
3837- position [ 0 ] += xFactor * dist * 0.5 ;
3838- var yFactor = ( neighbor . y - this . y ) ;
3839- if ( yFactor > 1 ) {
3840- yFactor = - 1 ;
3841- } else if ( yFactor < - 1 ) {
3842- yFactor = 1 ;
3843- }
3844- position [ 1 ] += yFactor * dist * 0.5 ;
3845- }
3846- return position ;
3847- } ;
3848-
3849- NodeSquare . prototype . getNeighbors = function getNeighbors ( ) {
3850- var neighbors = [ ] ;
3851- for ( var i = - 1 ; i <= 1 ; i ++ ) {
3852- var x = this . x + i ;
3853- if ( x < 0 ) {
3854- if ( this . som . torus ) {
3855- x = this . som . gridDim . x - 1 ;
3856- } else {
3857- continue ;
3858- }
3859- } else if ( x === this . som . gridDim . x ) {
3860- if ( this . som . torus ) {
3861- x = 0 ;
3862- } else {
3863- continue ;
3864- }
3865- }
3866- for ( var j = - 1 ; j <= 1 ; j ++ ) {
3867- if ( i === 0 && j === 0 ) {
3868- continue ;
3869- }
3870- var y = this . y + j ;
3871- if ( y < 0 ) {
3872- if ( this . som . torus ) {
3873- y = this . som . gridDim . y - 1 ;
3874- } else {
3875- continue ;
3876- }
3877- } else if ( y === this . som . gridDim . y ) {
3878- if ( this . som . torus ) {
3879- y = 0 ;
3880- } else {
3881- continue ;
3882- }
3883- }
3884- neighbors . push ( this . som . nodes [ x ] [ y ] ) ;
3885- }
3886- }
3887- return this . neighbors = neighbors ;
3888- } ;
3889-
3890- module . exports = NodeSquare ;
3891- } , { } ] , 59 :[ function ( require , module , exports ) {
38923768'use strict' ;
38933769
38943770var NodeSquare = require ( './node-square' ) ,
@@ -4310,5 +4186,143 @@ function getMaxDistance(distance, numWeights) {
43104186}
43114187
43124188module . exports = SOM ;
4313- } , { "./node-hexagonal" :57 , "./node-square" :58 } ] } , { } , [ 1 ] ) ( 1 )
4189+ } , { "./node-hexagonal" :57 , "./node-square" :58 } ] , 57 :[ function ( require , module , exports ) {
4190+ var NodeSquare = require ( './node-square' ) ;
4191+
4192+ function NodeHexagonal ( x , y , weights , som ) {
4193+
4194+ NodeSquare . call ( this , x , y , weights , som ) ;
4195+
4196+ this . hX = x - Math . floor ( y / 2 ) ;
4197+ this . z = 0 - this . hX - y ;
4198+
4199+ }
4200+
4201+ NodeHexagonal . prototype = new NodeSquare ;
4202+ NodeHexagonal . prototype . constructor = NodeHexagonal ;
4203+
4204+ NodeHexagonal . prototype . getDistance = function getDistanceHexagonal ( otherNode ) {
4205+ return Math . max ( Math . abs ( this . hX - otherNode . hX ) , Math . abs ( this . y - otherNode . y ) , Math . abs ( this . z - otherNode . z ) ) ;
4206+ } ;
4207+
4208+ NodeHexagonal . prototype . getDistanceTorus = function getDistanceTorus ( otherNode ) {
4209+ var distX = Math . abs ( this . hX - otherNode . hX ) ,
4210+ distY = Math . abs ( this . y - otherNode . y ) ,
4211+ distZ = Math . abs ( this . z - otherNode . z ) ;
4212+ 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 ) ) ;
4213+ } ;
4214+
4215+ NodeHexagonal . prototype . getPosition = function getPosition ( ) {
4216+ throw new Error ( 'Unimplemented : cannot get position of the points for hexagonal grid' ) ;
4217+ } ;
4218+
4219+ module . exports = NodeHexagonal ;
4220+ } , { "./node-square" :58 } ] , 58 :[ function ( require , module , exports ) {
4221+ function NodeSquare ( x , y , weights , som ) {
4222+ this . x = x ;
4223+ this . y = y ;
4224+ this . weights = weights ;
4225+ this . som = som ;
4226+ this . neighbors = { } ;
4227+ }
4228+
4229+ NodeSquare . prototype . adjustWeights = function adjustWeights ( target , learningRate , influence ) {
4230+ for ( var i = 0 , ii = this . weights . length ; i < ii ; i ++ ) {
4231+ this . weights [ i ] += learningRate * influence * ( target [ i ] - this . weights [ i ] ) ;
4232+ }
4233+ } ;
4234+
4235+ NodeSquare . prototype . getDistance = function getDistance ( otherNode ) {
4236+ return Math . max ( Math . abs ( this . x - otherNode . x ) , Math . abs ( this . y - otherNode . y ) ) ;
4237+ } ;
4238+
4239+ NodeSquare . prototype . getDistanceTorus = function getDistanceTorus ( otherNode ) {
4240+ var distX = Math . abs ( this . x - otherNode . x ) ,
4241+ distY = Math . abs ( this . y - otherNode . y ) ;
4242+ return Math . max ( Math . min ( distX , this . som . gridDim . x - distX ) , Math . min ( distY , this . som . gridDim . y - distY ) ) ;
4243+ } ;
4244+
4245+ NodeSquare . prototype . getNeighbors = function getNeighbors ( xy ) {
4246+ if ( ! this . neighbors [ xy ] ) {
4247+ this . neighbors [ xy ] = new Array ( 2 ) ;
4248+
4249+ // left or bottom neighbor
4250+ var v ;
4251+ if ( this [ xy ] > 0 ) {
4252+ v = this [ xy ] - 1 ;
4253+ } else if ( this . som . torus ) {
4254+ v = this . som . gridDim [ xy ] - 1
4255+ }
4256+ if ( typeof v !== 'undefined' ) {
4257+ var x , y ;
4258+ if ( xy === 'x' ) {
4259+ x = v ;
4260+ y = this . y ;
4261+ } else {
4262+ x = this . x ;
4263+ y = v ;
4264+ }
4265+ this . neighbors [ xy ] [ 0 ] = this . som . nodes [ x ] [ y ] ;
4266+ }
4267+
4268+ // top or right neighbor
4269+ var w ;
4270+ if ( this [ xy ] < ( this . som . gridDim [ xy ] - 1 ) ) {
4271+ w = this [ xy ] + 1 ;
4272+ } else if ( this . som . torus ) {
4273+ w = 0 ;
4274+ }
4275+ if ( typeof w !== 'undefined' ) {
4276+ if ( xy === 'x' ) {
4277+ x = w ;
4278+ y = this . y ;
4279+ } else {
4280+ x = this . x ;
4281+ y = w ;
4282+ }
4283+ this . neighbors [ xy ] [ 1 ] = this . som . nodes [ x ] [ y ] ;
4284+ }
4285+ }
4286+ return this . neighbors [ xy ] ;
4287+ } ;
4288+
4289+ NodeSquare . prototype . getPos = function getPos ( xy , element ) {
4290+ var neighbors = this . getNeighbors ( xy ) ,
4291+ distance = this . som . distance ,
4292+ bestNeighbor ,
4293+ direction ;
4294+ if ( neighbors [ 0 ] ) {
4295+ if ( neighbors [ 1 ] ) {
4296+ var dist1 = distance ( element , neighbors [ 0 ] . weights ) ,
4297+ dist2 = distance ( element , neighbors [ 1 ] . weights ) ;
4298+ if ( dist1 < dist2 ) {
4299+ bestNeighbor = neighbors [ 0 ] ;
4300+ direction = - 1 ;
4301+ } else {
4302+ bestNeighbor = neighbors [ 1 ] ;
4303+ direction = 1 ;
4304+ }
4305+ } else {
4306+ bestNeighbor = neighbors [ 0 ] ;
4307+ direction = - 1 ;
4308+ }
4309+ } else {
4310+ bestNeighbor = neighbors [ 1 ] ;
4311+ direction = 1 ;
4312+ }
4313+ var simA = 1 - distance ( element , this . weights ) ,
4314+ simB = 1 - distance ( element , bestNeighbor . weights ) ;
4315+ var factor = ( ( simA - simB ) / ( 2 - simA - simB ) ) ;
4316+ return 0.5 + 0.5 * factor * direction ;
4317+ } ;
4318+
4319+ NodeSquare . prototype . getPosition = function getPosition ( element ) {
4320+ return [
4321+ this . getPos ( 'x' , element ) ,
4322+ this . getPos ( 'y' , element )
4323+ ] ;
4324+ } ;
4325+
4326+ module . exports = NodeSquare ;
4327+ } , { } ] } , { } , [ 1 ] ) ( 1 )
43144328} ) ;
0 commit comments