Skip to content

Commit fab13ae

Browse files
committed
Fix lint errors and set read-only properties
1 parent be4d36f commit fab13ae

File tree

8 files changed

+100
-63
lines changed

8 files changed

+100
-63
lines changed

lib/node_modules/@stdlib/ml/online-binary-classification/lib/loss/modified_huber.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function modifiedHuber( weights, x, y, eta, lambda ) {
3838
if ( p < -1.0 ) {
3939
weights.add( x, ( eta * 4.0 * y ) );
4040
} else if ( p < 1.0 ) {
41-
weights.add( x, eta * ( y - p*y ) );
41+
weights.add( x, eta * ( y - (p*y) ) );
4242
}
4343
}
4444
} // end FUNCTION modifiedHuber()

lib/node_modules/@stdlib/ml/online-binary-classification/lib/weight_vector.js

+84-52
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,27 @@
88

99
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' );
1010
var isBoolean = require( '@stdlib/assert/is-boolean' );
11+
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
1112
var pow = require( '@stdlib/math/base/special/pow' );
1213
var dot = require( './dot.js' );
1314

1415

1516
// VARIABLES //
1617

17-
var MIN_SCALE = 1e-11;
18+
var MIN_SCALE = 1.0e-11;
1819

1920

20-
// MAIN //
21-
22-
/**
23-
* Creates a WeightVector.
24-
*
25-
* @constructor
26-
* @param {PositiveInteger} dim - number of feature weights (excluding bias/intercept term)
27-
* @param {boolean} intercept - boolean indicating whether a bias/intercept weight should be implicitly assumed
28-
*/
29-
function WeightVector( dim, intercept ) {
30-
var i;
31-
if ( !(this instanceof WeightVector) ) {
32-
return new WeightVector( dim, intercept );
33-
}
34-
if ( !isPositiveInteger( dim ) ) {
35-
throw new TypeError( 'invalid input argument. First argument `dim` must be a positive integer. Value: `' + dim + '`.' );
36-
}
37-
if ( !isBoolean( intercept ) ) {
38-
throw new TypeError( 'invalid input argument. Second argument `intercept` must be a boolean primitive. Value: `' + intercept + '`.' );
39-
}
40-
41-
this.scale = 1.0;
42-
this.norm = 0.0;
43-
this.intercept = intercept;
44-
this.nWeights = dim + ( this.intercept ? 1 : 0 );
45-
46-
this._data = new Array( this.nWeights );
47-
48-
// Initialize weights to zero:
49-
for ( i = 0; i < this.nWeights; i++ ) {
50-
this._data[ i ] = 0.0;
51-
}
52-
} // end FUNCTION WeightVector()
21+
// FUNCTIONS //
5322

5423
/**
5524
* Scale elements of the weight vector by the supplied factor.
5625
*
57-
* @memberof WeightVector.prototype
58-
* @function scaleTo
26+
* @private
5927
* @param {number} factor - scaling factor
6028
* @throws {RangeError} `lambda` times `eta` must be large enough for the scaling weight to be nonnegative
6129
*/
62-
WeightVector.prototype.scaleTo = function scaleTo( factor ) {
30+
function scaleTo( factor ) {
31+
/* eslint-disable no-invalid-this */
6332
var i;
6433
if ( this.scale < MIN_SCALE ) {
6534
// Scale vector to one:
@@ -76,23 +45,22 @@ WeightVector.prototype.scaleTo = function scaleTo( factor ) {
7645
} else {
7746
throw new RangeError( 'Scaling weight vector by nonpositive value, likely due to too large value of eta * lambda. Value: `' + factor + '`' );
7847
}
79-
}; // end METHOD scaleTo()
80-
48+
} // end FUNCTION scaleTo()
8149

8250
/**
8351
* Adds vector `x` to the weight vector after scaling its elements.
8452
*
85-
* @memberof WeightVector.prototype
86-
* @function add
53+
* @private
8754
* @param {NumericArray} x - vector to add
8855
* @param {number} [xScale=1.0] - number to scale the elements of x with
8956
*/
90-
WeightVector.prototype.add = function add( x, xScale ) {
57+
function add( x, xScale ) {
58+
/* eslint-disable no-invalid-this */
9159
var xscaled;
9260
var inner;
9361
var i;
9462

95-
inner = 0;
63+
inner = 0.0;
9664
if ( xScale === void 0 ) {
9765
xScale = 1.0;
9866
}
@@ -105,23 +73,22 @@ WeightVector.prototype.add = function add( x, xScale ) {
10573
if ( this.intercept ) {
10674
xscaled = 1.0 * xScale;
10775
inner += this._data[ i ] * xscaled;
108-
this._data[ i ] = this._data[ i ] + xscaled / this.scale;
76+
this._data[ i ] = this._data[ i ] + ( xscaled / this.scale );
10977
}
110-
this.norm += ( dot( x, x ) + ( this.intercept ? 1.0 : 0.0 ) ) *
111-
pow( xScale, 2 ) +
78+
this.norm += ( ( dot( x, x ) + ( this.intercept ? 1.0 : 0.0 ) ) *
79+
pow( xScale, 2 ) ) +
11280
( 2.0 * this.scale * inner );
113-
}; // end METHOD add()
114-
81+
} // end FUNCTION add()
11582

11683
/**
11784
* Calculates the inner product of the weights and supplied vector `x`.
11885
*
119-
* @memberof WeightVector.prototype
120-
* @function innerProduct
86+
* @private
12187
* @param {NumericArray} x - input vector
12288
* @returns {number} inner product
12389
*/
124-
WeightVector.prototype.innerProduct = function innerProduct( x ) {
90+
function innerProduct( x ) {
91+
/* eslint-disable no-invalid-this */
12592
var ret = 0;
12693
var i;
12794
for ( i = 0; i < x.length; i++ ) {
@@ -130,7 +97,72 @@ WeightVector.prototype.innerProduct = function innerProduct( x ) {
13097
ret += this.intercept ? this._data[ i ] : 0.0;
13198
ret *= this.scale;
13299
return ret;
133-
}; // end METHOD innerProduct()
100+
} // end FUNCTION innerProduct()
101+
102+
103+
// MAIN //
104+
105+
/**
106+
* Creates a WeightVector.
107+
*
108+
* @constructor
109+
* @param {PositiveInteger} dim - number of feature weights (excluding bias/intercept term)
110+
* @param {boolean} intercept - boolean indicating whether a bias/intercept weight should be implicitly assumed
111+
*/
112+
function WeightVector( dim, intercept ) {
113+
var i;
114+
if ( !(this instanceof WeightVector) ) {
115+
return new WeightVector( dim, intercept );
116+
}
117+
if ( !isPositiveInteger( dim ) ) {
118+
throw new TypeError( 'invalid input argument. First argument `dim` must be a positive integer. Value: `' + dim + '`.' );
119+
}
120+
if ( !isBoolean( intercept ) ) {
121+
throw new TypeError( 'invalid input argument. Second argument `intercept` must be a boolean primitive. Value: `' + intercept + '`.' );
122+
}
123+
124+
this.scale = 1.0;
125+
this.norm = 0.0;
126+
this.intercept = intercept;
127+
this.nWeights = dim + ( this.intercept ? 1 : 0 );
128+
129+
this._data = new Array( this.nWeights );
130+
131+
// Initialize weights to zero:
132+
for ( i = 0; i < this.nWeights; i++ ) {
133+
this._data[ i ] = 0.0;
134+
}
135+
} // end FUNCTION WeightVector()
136+
137+
/**
138+
* Scale elements of the weight vector by the supplied factor.
139+
*
140+
* @memberof WeightVector.prototype
141+
* @function scaleTo
142+
* @param {number} factor - scaling factor
143+
* @throws {RangeError} `lambda` times `eta` must be large enough for the scaling weight to be nonnegative
144+
*/
145+
setReadOnly( WeightVector.prototype, 'scaleTo', scaleTo );
146+
147+
/**
148+
* Adds vector `x` to the weight vector after scaling its elements.
149+
*
150+
* @memberof WeightVector.prototype
151+
* @function add
152+
* @param {NumericArray} x - vector to add
153+
* @param {number} [xScale=1.0] - number to scale the elements of x with
154+
*/
155+
setReadOnly( WeightVector.prototype, 'add', add );
156+
157+
/**
158+
* Calculates the inner product of the weights and supplied vector `x`.
159+
*
160+
* @memberof WeightVector.prototype
161+
* @function innerProduct
162+
* @param {NumericArray} x - input vector
163+
* @returns {number} inner product
164+
*/
165+
setReadOnly( WeightVector.prototype, 'innerProduct', innerProduct );
134166

135167

136168
// EXPORTS //

lib/node_modules/@stdlib/ml/online-binary-classification/test/test.learner.js

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ tape( 'the `predict` method calculates prediction probabilities when `type` is s
249249

250250
x = [ 2.0, 2.0 ];
251251
actual = model.predict( x, 'probability' );
252+
252253
// Add intercept...
253254
x.push( 1.0 );
254255
wx = 0;

lib/node_modules/@stdlib/ml/online-binary-classification/test/test.weight_vector.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ tape( 'the `innerProduct` method calculates the inner product of the weights and
111111
});
112112

113113
tape( 'the `add` method adds a vector `x` to the weights', function test( t ) {
114+
/* eslint-disable no-underscore-dangle */
114115
var expected;
115116
var weights;
116117

@@ -166,7 +167,7 @@ tape( 'the `scaleTo` method scales the weights to one if the internal scale is s
166167
weights.scaleTo( 1.0 );
167168

168169
t.equal( weights.scale, 1.0, 'scale is equal to 1.0' );
169-
t.deepEqual( weights._data, [ 2e-13, 2e-13, 2e-13 ], '_data array is equal to [2e-13,2e-13,2e-13]' );
170+
t.deepEqual( weights._data, [ 2e-13, 2e-13, 2e-13 ], '_data array is equal to [2e-13,2e-13,2e-13]' ); // eslint-disable-line no-underscore-dangle
170171
t.end();
171172
});
172173

lib/node_modules/@stdlib/ml/online-sgd-regression/lib/weight_vector.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' );
1010
var isBoolean = require( '@stdlib/assert/is-boolean' );
11+
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
1112
var pow = require( '@stdlib/math/base/special/pow' );
1213
var dot = require( './dot.js' );
1314

@@ -141,7 +142,7 @@ function WeightVector( dim, intercept ) {
141142
* @param {number} factor - scaling factor
142143
* @throws {RangeError} `lambda` times `eta` must be large enough for the scaling weight to be nonnegative
143144
*/
144-
WeightVector.prototype.scaleTo = scaleTo;
145+
setReadOnly( WeightVector.prototype, 'scaleTo', scaleTo );
145146

146147
/**
147148
* Adds vector `x` to the weight vector after scaling its elements.
@@ -151,7 +152,7 @@ WeightVector.prototype.scaleTo = scaleTo;
151152
* @param {NumericArray} x - vector to add
152153
* @param {number} [xScale=1.0] - number to scale the elements of x with
153154
*/
154-
WeightVector.prototype.add = add;
155+
setReadOnly( WeightVector.prototype, 'add', add );
155156

156157
/**
157158
* Calculates the inner product of the weights and supplied vector `x`.
@@ -161,7 +162,7 @@ WeightVector.prototype.add = add;
161162
* @param {NumericArray} x - input vector
162163
* @returns {number} inner product
163164
*/
164-
WeightVector.prototype.innerProduct = innerProduct;
165+
setReadOnly( WeightVector.prototype, 'innerProduct', innerProduct );
165166

166167

167168
// EXPORTS //

lib/node_modules/@stdlib/ml/online-sgd-regression/test/test.loss.epsilon_insensitive.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tape( 'the weights are unchanged for errors smaller than epsilon in magnitude (n
2727
lambda = 0.0;
2828

2929
weights = new WeightVector( 3, false );
30-
weights.add( [1.0,2.0,3.0] );
30+
weights.add( [ 1.0, 2.0, 3.0 ] );
3131
epsilon = 0.1;
3232
eta = 0.02;
3333

@@ -37,12 +37,13 @@ tape( 'the weights are unchanged for errors smaller than epsilon in magnitude (n
3737
expected = [ 1.0, 2.0, 3.0 ];
3838

3939
epsilonInsensitiveLoss( weights, y, x, eta, lambda, epsilon );
40-
t.deepEqual( weights._data, expected, 'weights are correctly updated' );
40+
t.deepEqual( weights._data, expected, 'weights are correctly updated' ); // eslint-disable-line no-underscore-dangle
4141

4242
t.end();
4343
});
4444

4545
tape( 'the sub-gradient of the linear loss times the learning rate is added to the weights for absolute errors greater or equal than epsilon (no regularization)', function test( t ) {
46+
/* eslint-disable no-underscore-dangle */
4647
var expected;
4748
var weights;
4849
var epsilon;
@@ -54,7 +55,7 @@ tape( 'the sub-gradient of the linear loss times the learning rate is added to t
5455
lambda = 0.0;
5556

5657
weights = new WeightVector( 3, false );
57-
weights.add( [1.0,2.0,3.0] );
58+
weights.add( [ 1.0, 2.0, 3.0 ] );
5859
epsilon = 0.1;
5960
eta = 0.02;
6061

lib/node_modules/@stdlib/ml/online-sgd-regression/test/test.loss.huber.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ tape( 'the sub-gradient of the squared-error loss times the learning rate is add
4040

4141
huberLoss( weights, x, y, eta, lambda, epsilon );
4242

43-
t.deepEqual( weights._data, expected, 'weights are correctly updated' );
43+
t.deepEqual( weights._data, expected, 'weights are correctly updated' ); // eslint-disable-line no-underscore-dangle
4444
t.end();
4545
});
4646

4747
tape( 'the sub-gradient of the linear loss times the learning rate is added to the weights for absolute errors greater or equal than epsilon (no regularization)', function test( t ) {
48+
/* eslint-disable no-underscore-dangle */
4849
var expected;
4950
var weights;
5051
var epsilon;

lib/node_modules/@stdlib/ml/online-sgd-regression/test/test.loss.squared_error.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tape( 'the sub-gradient of the squared-error loss times the learning rate is add
2727
lambda = 0.0;
2828

2929
weights = new WeightVector( 3, false );
30-
weights.add( [1.0,2.0,3.0] );
30+
weights.add( [ 1.0, 2.0, 3.0 ] );
3131
epsilon = 0.1;
3232
eta = 0.02;
3333

@@ -39,6 +39,6 @@ tape( 'the sub-gradient of the squared-error loss times the learning rate is add
3939

4040
squaredErrorLoss( weights, x, y, eta, lambda, epsilon );
4141

42-
t.deepEqual( weights._data, expected, 'weights are correctly updated' );
42+
t.deepEqual( weights._data, expected, 'weights are correctly updated' ); // eslint-disable-line no-underscore-dangle
4343
t.end();
4444
});

0 commit comments

Comments
 (0)