11/**
22 * ml - Machine learning tools
3- * @version v0.3.9
3+ * @version v0.3.10
44 * @link https://github.com/mljs/ml
55 * @license MIT
66 */
@@ -3475,7 +3475,7 @@ module.exports = {
34753475function getEquallySpacedData ( x , y , options ) {
34763476
34773477 var xLength = x . length ;
3478- if ( x . length !== y . length )
3478+ if ( xLength !== y . length )
34793479 throw new RangeError ( "the x and y vector doesn't have the same size." ) ;
34803480
34813481 if ( options === undefined ) options = { } ;
@@ -3496,6 +3496,24 @@ function getEquallySpacedData(x, y, options) {
34963496
34973497 var algorithm = options . variant === "slot" ? "slot" : "smooth" ; // default value: smooth
34983498
3499+ var output = algorithm === "slot" ? getEquallySpacedSlot ( x , y , from , to , numberOfPoints ) : getEquallySpacedSmooth ( x , y , from , to , numberOfPoints ) ;
3500+
3501+ return reverse ? output . reverse ( ) : output ;
3502+ }
3503+
3504+ /**
3505+ * function that retrieves the getEquallySpacedData with the variant "smooth"
3506+ *
3507+ * @param x
3508+ * @param y
3509+ * @param from - Initial point
3510+ * @param to - Final point
3511+ * @param numberOfPoints
3512+ * @returns {Array } - Array of y's equally spaced with the variant "smooth"
3513+ */
3514+ function getEquallySpacedSmooth ( x , y , from , to , numberOfPoints ) {
3515+ var xLength = x . length ;
3516+
34993517 var step = ( to - from ) / ( numberOfPoints - 1 ) ;
35003518 var halfStep = step / 2 ;
35013519
@@ -3520,36 +3538,20 @@ function getEquallySpacedData(x, y, options) {
35203538 var sumAtMin = 0 ;
35213539 var sumAtMax = 0 ;
35223540
3523- // for slot algorithm
3524- var currentPoints = 0 ;
3525-
35263541 var i = 0 ; // index of input
35273542 var j = 0 ; // index of output
35283543
3529- function getValue ( ) {
3530- if ( algorithm === "smooth" )
3531- return integral ( previousX , nextX , slope , intercept ) ;
3532- else
3533- return previousY ;
3534- }
3535-
3536- function updateParameters ( ) {
3537- slope = getSlope ( previousX , previousY , nextX , nextY ) ;
3538- intercept = - slope * previousX + previousY ;
3539- }
3540-
35413544 function getSlope ( x0 , y0 , x1 , y1 ) {
35423545 return ( y1 - y0 ) / ( x1 - x0 ) ;
35433546 }
35443547
35453548 main: while ( true ) {
35463549 while ( nextX - max >= 0 ) {
35473550 // no overlap with original point, just consume current value
3548- var add = algorithm === "smooth" ? integral ( 0 , max - previousX , slope , previousY ) : previousY ;
3551+ var add = integral ( 0 , max - previousX , slope , previousY ) ;
35493552 sumAtMax = currentValue + add ;
35503553
3551- var divisor = algorithm === "smooth" ? step : currentPoints - 1 ;
3552- output [ j ] = ( sumAtMax - sumAtMin ) / divisor ;
3554+ output [ j ] = ( sumAtMax - sumAtMin ) / step ;
35533555 j ++ ;
35543556
35553557 if ( j === numberOfPoints )
@@ -3558,20 +3560,14 @@ function getEquallySpacedData(x, y, options) {
35583560 min = max ;
35593561 max += step ;
35603562 sumAtMin = sumAtMax ;
3561- if ( algorithm === "slot" )
3562- currentPoints = 0 ;
35633563 }
35643564
35653565 if ( previousX <= min && min <= nextX ) {
3566- add = algorithm === "smooth" ? integral ( 0 , min - previousX , slope , previousY ) : previousY ;
3566+ add = integral ( 0 , min - previousX , slope , previousY ) ;
35673567 sumAtMin = currentValue + add ;
3568- if ( algorithm === "slot" )
3569- currentPoints ++ ;
35703568 }
35713569
3572- currentValue += getValue ( ) ;
3573- if ( currentPoints !== 0 )
3574- currentPoints ++ ;
3570+ currentValue += integral ( previousX , nextX , slope , intercept ) ;
35753571
35763572 previousX = nextX ;
35773573 previousY = nextY ;
@@ -3584,11 +3580,96 @@ function getEquallySpacedData(x, y, options) {
35843580 nextX += lastOriginalStep ;
35853581 nextY = 0 ;
35863582 }
3583+ // updating parameters
3584+ slope = getSlope ( previousX , previousY , nextX , nextY ) ;
3585+ intercept = - slope * previousX + previousY ;
3586+ }
3587+
3588+ return output ;
3589+ }
3590+
3591+ /**
3592+ * function that retrieves the getEquallySpacedData with the variant "slot"
3593+ *
3594+ * @param x
3595+ * @param y
3596+ * @param from - Initial point
3597+ * @param to - Final point
3598+ * @param numberOfPoints
3599+ * @returns {Array } - Array of y's equally spaced with the variant "slot"
3600+ */
3601+ function getEquallySpacedSlot ( x , y , from , to , numberOfPoints ) {
3602+ var xLength = x . length ;
3603+
3604+ var step = ( to - from ) / ( numberOfPoints - 1 ) ;
3605+ var halfStep = step / 2 ;
3606+ var lastStep = x [ x . length - 1 ] - x [ x . length - 2 ] ;
3607+
3608+ var start = from - halfStep ;
3609+ var output = new Array ( numberOfPoints ) ;
3610+
3611+ // Init main variables
3612+ var min = start ;
3613+ var max = start + step ;
35873614
3588- updateParameters ( ) ;
3615+ var previousX = - Number . MAX_VALUE ;
3616+ var previousY = 0 ;
3617+ var nextX = x [ 0 ] ;
3618+ var nextY = y [ 0 ] ;
3619+ var frontOutsideSpectra = 0 ;
3620+ var backOutsideSpectra = true ;
3621+
3622+ var currentValue = 0 ;
3623+
3624+ // for slot algorithm
3625+ var currentPoints = 0 ;
3626+
3627+ var i = 1 ; // index of input
3628+ var j = 0 ; // index of output
3629+
3630+ main: while ( true ) {
3631+ while ( previousX - max > 0 ) {
3632+ // no overlap with original point, just consume current value
3633+ if ( backOutsideSpectra ) {
3634+ currentPoints ++ ;
3635+ backOutsideSpectra = false ;
3636+ }
3637+
3638+ output [ j ] = currentPoints <= 0 ? 0 : currentValue / currentPoints ;
3639+ j ++ ;
3640+
3641+ if ( j === numberOfPoints )
3642+ break main;
3643+
3644+ min = max ;
3645+ max += step ;
3646+ currentValue = 0 ;
3647+ currentPoints = 0 ;
3648+ }
3649+
3650+ if ( previousX > min ) {
3651+ currentValue += previousY ;
3652+ currentPoints ++ ;
3653+ }
3654+
3655+ if ( previousX === - Number . MAX_VALUE || frontOutsideSpectra > 1 )
3656+ currentPoints -- ;
3657+
3658+ previousX = nextX ;
3659+ previousY = nextY ;
3660+
3661+ if ( i < xLength ) {
3662+ nextX = x [ i ] ;
3663+ nextY = y [ i ] ;
3664+ i ++ ;
3665+ } else {
3666+ nextX += lastStep ;
3667+ nextY = 0 ;
3668+ frontOutsideSpectra ++ ;
3669+ }
35893670 }
35903671
3591- return reverse ? output . reverse ( ) : output ;
3672+ return output ;
35923673}
35933674/**
35943675 * Function that calculates the integral of the line between two
@@ -3617,6 +3698,13 @@ exports.SNV = SNV;
36173698var Stat = require ( 'ml-stat' ) ;
36183699var Matrix = require ( 'ml-matrix' ) ;
36193700
3701+ /**
3702+ * Function that applies the standard normal variate (SNV) to each row vector of y's
3703+ * values.
3704+ *
3705+ * @param data - Matrix of y vectors
3706+ * @returns {Matrix }
3707+ */
36203708function SNV ( data ) {
36213709 var Y = data ;
36223710 if ( ! Matrix . isMatrix ( data ) ) {
@@ -4474,8 +4562,10 @@ FeedforwardNeuralNetwork.prototype.iteration = function (data, prediction, learn
44744562 error [ i ] = prediction [ i ] - forwardResult [ i ] ;
44754563 }
44764564
4477- for ( i = this . layers . length - 1 ; i >= 0 ; i -- ) {
4478- error = this . layers [ i ] . train ( error , learningRate , momentum ) ;
4565+ var lengthLayers = this . layers . length ;
4566+
4567+ for ( i = 0 ; i < lengthLayers ; ++ i ) {
4568+ error = this . layers [ lengthLayers - 1 - i ] . train ( error , learningRate , momentum ) ;
44794569 }
44804570} ;
44814571
0 commit comments