Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions lib/node_modules/@stdlib/ml/incr/lvq/lib/add_proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var dcopy = require( '@stdlib/blas/base/dcopy' );
var copyVector = require( './copy_vector.js' );
var createIntVector = require( './int_vector.js' );
var createMatrix = require( './matrix.js' );


// MAIN //

/**
* Adds a new prototype with the given data point and label.
*
* @private
* @param {PositiveInteger} k - current number of prototypes
* @param {PositiveInteger} ndims - number of dimensions
* @param {ndarray} protos - matrix containing existing prototypes
* @param {ndarray} protoLabels - vector containing class labels for existing prototypes
* @param {ndarray} vec - data vector to use as new prototype
* @param {integer} label - class label for new prototype
* @returns {Object} object containing new protos and protoLabels

Check warning on line 41 in lib/node_modules/@stdlib/ml/incr/lvq/lib/add_proto.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "protos"
*/
function addPrototype( k, ndims, protos, protoLabels, vec, label ) {
var newProtos;
var newLabels;
var vbuf;
var sv;
var ov;
var i;

newProtos = createMatrix( k + 1, ndims, true );
newLabels = createIntVector( k + 1, true );

if ( k > 0 && protos ) {
dcopy( protos.data.length, protos.data, 1, newProtos.data, 1 );
copyVector( newLabels, protoLabels );
}

vbuf = vec.data;
sv = vec.strides[ 0 ];
ov = vec.offset;

for ( i = 0; i < ndims; i++ ) {
newProtos.data[ ( k * ndims ) + i ] = vbuf[ ov + ( i * sv ) ];
}

newLabels.data[ newLabels.offset + ( k * newLabels.strides[ 0 ] ) ] = label;

return {
'protos': newProtos,
'protoLabels': newLabels
};
}

module.exports = addPrototype;
75 changes: 75 additions & 0 deletions lib/node_modules/@stdlib/ml/incr/lvq/lib/copy_matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray;


// MAIN //

/**
* Copies matrix elements to another matrix.
*
* @private
* @param {ndarray} Y - destination matrix
* @param {ndarray} X - source matrix
* @returns {ndarray} destination matrix
*/
function copyMatrix( Y, X ) {
var xbuf;
var ybuf;
var sx1;
var sx2;
var sy1;
var sy2;
var ox;
var oy;
var M;
var N;
var i;

M = X.shape[ 0 ];
N = X.shape[ 1 ];

xbuf = X.data;
ybuf = Y.data;

sx1 = X.strides[ 0 ];
sx2 = X.strides[ 1 ];

sy1 = Y.strides[ 0 ];
sy2 = Y.strides[ 1 ];

ox = X.offset;
oy = Y.offset;

for ( i = 0; i < M; i++ ) {
gcopy( N, xbuf, sx2, ox, ybuf, sy2, oy );
ox += sx1;
oy += sy1;
}
return Y;
}


// EXPORTS //

module.exports = copyMatrix;
44 changes: 44 additions & 0 deletions lib/node_modules/@stdlib/ml/incr/lvq/lib/copy_vector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray;


// MAIN //

/**
* Copies vector elements to another vector.
*
* @private
* @param {ndarray} out - destination vector
* @param {ndarray} src - source vector
* @returns {ndarray} destination vector
*/
function copyVector( out, src ) {
gcopy( src.shape[0], src.data, src.strides[0], src.offset, out.data, out.strides[0], out.offset ); // eslint-disable-line max-len
return out;
}


// EXPORTS //

module.exports = copyVector;
63 changes: 63 additions & 0 deletions lib/node_modules/@stdlib/ml/incr/lvq/lib/find_closest_proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var PINF = require( '@stdlib/constants/float64/pinf' );


// MAIN //

/**
* Finds the closest prototypes.
*
* @private
* @param {Function} dist - distance function
* @param {PositiveInteger} k - number of clusters
* @param {PositiveInteger} ndims - number of dimensions
* @param {NumericArray} P - strided array containing prototypes
* @param {PositiveInteger} sp - prototype row stride
* @param {NonNegativeInteger} op - prototype index offset
* @param {NumericArray} X - strided array containing a data point
* @param {integer} sx - data point stride
* @param {NonNegativeInteger} ox - data point index offset
* @returns {NonNegativeInteger} prototype index
*/
function closestPrototype( dist, k, ndims, P, sp, op, X, sx, ox ) {
var mindist;
var mini;
var d;
var i;

mindist = PINF;
mini = -1;
for (i = 0; i < k; ++i) {
d = dist( ndims, P, 1, op, X, sx, ox );

if (d < mindist) {
mindist = d;
mini = i;
}
op += sp;
}
return mini;
}

module.exports = closestPrototype;
75 changes: 75 additions & 0 deletions lib/node_modules/@stdlib/ml/incr/lvq/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Incrementally perform supervised classification using lvq1.
*
* @module @stdlib/ml/incr/lvq
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var Int32Array = require( '@stdlib/array/int32' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
* var incrlvq = require( '@stdlib/ml/incr/lvq' );
*
* // Define initial prototype locations (2 prototypes, 2 dimensions):
* var buffer = new Float64Array( [ 0.0, 0.0, 1.0, 1.0 ] );
* var prototypes = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
*
* // Define prototype labels:
* var labelBuffer = new Int32Array( [ 0, 1 ] );
* var labels = ndarray( 'int32', labelBuffer, [ 2 ], [ 1 ], 0, 'row-major' );
*
* // Create an LVQ accumulator:
* var accumulator = incrlvq( prototypes, labels );
*
* var out = accumulator();
* // returns {...}
*
* // Create a data vector:
* var vecBuffer = new Float64Array( 2 );
* var vec = ndarray( 'float64', vecBuffer, [ 2 ], [ 1 ], 0, 'row-major' );
*
* // Provide labeled data to the accumulator:
* vec.set( 0, 0.5 );
* vec.set( 1, 0.5 );
*
* out = accumulator( vec, 0 ); // data point with label 0
* // returns {...}
*
* vec.set( 0, 1.5 );
* vec.set( 1, 1.5 );
*
* out = accumulator( vec, 1 ); // data point with label 1
* // returns {...}
*
* // Retrieve the current results:
* out = accumulator();
* // returns {...}
*/

// MAIN //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
58 changes: 58 additions & 0 deletions lib/node_modules/@stdlib/ml/incr/lvq/lib/int_vector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var Int32Array = require( '@stdlib/array/int32' );
var ctor = require( '@stdlib/ndarray/ctor' );
var bctor = require( '@stdlib/ndarray/base/ctor' );


// MAIN //

/**
* Returns an integer vector.
*
* @private
* @param {PositiveInteger} N - number of elements
* @param {boolean} bool - boolean indicating whether to create a low-level ndarray
* @returns {ndarray} vector
*/
function createIntVector( N, bool ) {
var strides;
var buffer;
var shape;
var f;

if ( bool ) {
f = bctor;
} else {
f = ctor;
}
buffer = new Int32Array( N );
shape = [ N ];
strides = [ 1 ];
return f( 'int32', buffer, shape, strides, 0, 'row-major' );
}


// EXPORTS //

module.exports = createIntVector;
Loading
Loading