Skip to content

Commit 75bf3cc

Browse files
feat: add ml/incr/lvq
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent f7509ad commit 75bf3cc

14 files changed

Lines changed: 1156 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var dcopy = require( '@stdlib/blas/base/dcopy' );
24+
var copyVector = require( './copy_vector.js' );
25+
var createIntVector = require( './int_vector.js' );
26+
var createMatrix = require( './matrix.js' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Adds a new prototype with the given data point and label.
33+
*
34+
* @private
35+
* @param {PositiveInteger} k - current number of prototypes
36+
* @param {PositiveInteger} ndims - number of dimensions
37+
* @param {ndarray} protos - matrix containing existing prototypes
38+
* @param {ndarray} protoLabels - vector containing class labels for existing prototypes
39+
* @param {ndarray} vec - data vector to use as new prototype
40+
* @param {integer} label - class label for new prototype
41+
* @returns {Object} object containing new protos and protoLabels
42+
*/
43+
function addPrototype( k, ndims, protos, protoLabels, vec, label ) {
44+
var newProtos;
45+
var newLabels;
46+
var vbuf;
47+
var sv;
48+
var ov;
49+
var i;
50+
51+
newProtos = createMatrix( k + 1, ndims, true );
52+
newLabels = createIntVector( k + 1, true );
53+
54+
if ( k > 0 && protos ) {
55+
dcopy( protos.data.length, protos.data, 1, newProtos.data, 1 );
56+
copyVector( newLabels, protoLabels );
57+
}
58+
59+
vbuf = vec.data;
60+
sv = vec.strides[ 0 ];
61+
ov = vec.offset;
62+
63+
for ( i = 0; i < ndims; i++ ) {
64+
newProtos.data[ ( k * ndims ) + i ] = vbuf[ ov + ( i * sv ) ];
65+
}
66+
67+
newLabels.data[ newLabels.offset + ( k * newLabels.strides[ 0 ] ) ] = label;
68+
69+
return {
70+
'protos': newProtos,
71+
'protoLabels': newLabels
72+
};
73+
}
74+
75+
module.exports = addPrototype;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray;
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Copies matrix elements to another matrix.
30+
*
31+
* @private
32+
* @param {ndarray} Y - destination matrix
33+
* @param {ndarray} X - source matrix
34+
* @returns {ndarray} destination matrix
35+
*/
36+
function copyMatrix( Y, X ) {
37+
var xbuf;
38+
var ybuf;
39+
var sx1;
40+
var sx2;
41+
var sy1;
42+
var sy2;
43+
var ox;
44+
var oy;
45+
var M;
46+
var N;
47+
var i;
48+
49+
M = X.shape[ 0 ];
50+
N = X.shape[ 1 ];
51+
52+
xbuf = X.data;
53+
ybuf = Y.data;
54+
55+
sx1 = X.strides[ 0 ];
56+
sx2 = X.strides[ 1 ];
57+
58+
sy1 = Y.strides[ 0 ];
59+
sy2 = Y.strides[ 1 ];
60+
61+
ox = X.offset;
62+
oy = Y.offset;
63+
64+
for ( i = 0; i < M; i++ ) {
65+
gcopy( N, xbuf, sx2, ox, ybuf, sy2, oy );
66+
ox += sx1;
67+
oy += sy1;
68+
}
69+
return Y;
70+
}
71+
72+
73+
// EXPORTS //
74+
75+
module.exports = copyMatrix;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray;
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Copies vector elements to another vector.
30+
*
31+
* @private
32+
* @param {ndarray} out - destination vector
33+
* @param {ndarray} src - source vector
34+
* @returns {ndarray} destination vector
35+
*/
36+
function copyVector( out, src ) {
37+
gcopy( src.shape[0], src.data, src.strides[0], src.offset, out.data, out.strides[0], out.offset ); // eslint-disable-line max-len
38+
return out;
39+
}
40+
41+
42+
// EXPORTS //
43+
44+
module.exports = copyVector;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var PINF = require( '@stdlib/constants/float64/pinf' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Finds the closest prototypes.
30+
*
31+
* @private
32+
* @param {Function} dist - distance function
33+
* @param {PositiveInteger} k - number of clusters
34+
* @param {PositiveInteger} ndims - number of dimensions
35+
* @param {NumericArray} P - strided array containing prototypes
36+
* @param {PositiveInteger} sp - prototype row stride
37+
* @param {NonNegativeInteger} op - prototype index offset
38+
* @param {NumericArray} X - strided array containing a data point
39+
* @param {integer} sx - data point stride
40+
* @param {NonNegativeInteger} ox - data point index offset
41+
* @returns {NonNegativeInteger} prototype index
42+
*/
43+
function closestPrototype( dist, k, ndims, P, sp, op, X, sx, ox ) {
44+
var mindist;
45+
var mini;
46+
var d;
47+
var i;
48+
49+
mindist = PINF;
50+
mini = -1;
51+
for (i = 0; i < k; ++i) {
52+
d = dist( ndims, P, 1, op, X, sx, ox );
53+
54+
if (d < mindist) {
55+
mindist = d;
56+
mini = i;
57+
}
58+
op += sp;
59+
}
60+
return mini;
61+
}
62+
63+
module.exports = closestPrototype;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Incrementally perform supervised classification using lvq1.
23+
*
24+
* @module @stdlib/ml/incr/lvq
25+
*
26+
* @example
27+
* var Float64Array = require( '@stdlib/array/float64' );
28+
* var Int32Array = require( '@stdlib/array/int32' );
29+
* var ndarray = require( '@stdlib/ndarray/ctor' );
30+
* var incrlvq = require( '@stdlib/ml/incr/lvq' );
31+
*
32+
* // Define initial prototype locations (2 prototypes, 2 dimensions):
33+
* var buffer = new Float64Array( [ 0.0, 0.0, 1.0, 1.0 ] );
34+
* var prototypes = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
35+
*
36+
* // Define prototype labels:
37+
* var labelBuffer = new Int32Array( [ 0, 1 ] );
38+
* var labels = ndarray( 'int32', labelBuffer, [ 2 ], [ 1 ], 0, 'row-major' );
39+
*
40+
* // Create an LVQ accumulator:
41+
* var accumulator = incrlvq( prototypes, labels );
42+
*
43+
* var out = accumulator();
44+
* // returns {...}
45+
*
46+
* // Create a data vector:
47+
* var vecBuffer = new Float64Array( 2 );
48+
* var vec = ndarray( 'float64', vecBuffer, [ 2 ], [ 1 ], 0, 'row-major' );
49+
*
50+
* // Provide labeled data to the accumulator:
51+
* vec.set( 0, 0.5 );
52+
* vec.set( 1, 0.5 );
53+
*
54+
* out = accumulator( vec, 0 ); // data point with label 0
55+
* // returns {...}
56+
*
57+
* vec.set( 0, 1.5 );
58+
* vec.set( 1, 1.5 );
59+
*
60+
* out = accumulator( vec, 1 ); // data point with label 1
61+
* // returns {...}
62+
*
63+
* // Retrieve the current results:
64+
* out = accumulator();
65+
* // returns {...}
66+
*/
67+
68+
// MAIN //
69+
70+
var main = require( './main.js' );
71+
72+
73+
// EXPORTS //
74+
75+
module.exports = main;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var Int32Array = require( '@stdlib/array/int32' );
24+
var ctor = require( '@stdlib/ndarray/ctor' );
25+
var bctor = require( '@stdlib/ndarray/base/ctor' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Returns an integer vector.
32+
*
33+
* @private
34+
* @param {PositiveInteger} N - number of elements
35+
* @param {boolean} bool - boolean indicating whether to create a low-level ndarray
36+
* @returns {ndarray} vector
37+
*/
38+
function createIntVector( N, bool ) {
39+
var strides;
40+
var buffer;
41+
var shape;
42+
var f;
43+
44+
if ( bool ) {
45+
f = bctor;
46+
} else {
47+
f = ctor;
48+
}
49+
buffer = new Int32Array( N );
50+
shape = [ N ];
51+
strides = [ 1 ];
52+
return f( 'int32', buffer, shape, strides, 0, 'row-major' );
53+
}
54+
55+
56+
// EXPORTS //
57+
58+
module.exports = createIntVector;

0 commit comments

Comments
 (0)