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
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 //


// MAIN //

/**
* Find closest centroid.
* @private
* @param {Function} dist - distance method.
* @param {PositiveInteger} N - number of features.
* @param {PositiveInteger} k - number of clusters.
* @param {Float64Array} X - input strided matrix.
* @param {integer} sx - stride length of X.
* @param {integer} ox - starting index of X.
* @param {Float64Array} c - strided array centroid locations.
* @param {integer} sc1 - stride of the third dimension.
* @param {integer} sc2 - stride of second dimension.
* @param {integer} oc - initial index of centroids.
* @returns {NonNegativeInteger} closest centroid.
*/
function closestCentroid( dist, N, k, X, sx, ox, c, sc1, sc2, oc ) {
var bestDist;
var best;
var d;
var i;

best = 0;
bestDist = dist( N, X, sx, ox, c, sc2, oc );

oc += sc1; // move to the next centroid
for ( i = 1; i < k; i++ ) {
d = dist( N, X, sx, ox, c, sc2, oc );
if ( d < bestDist ) {
bestDist = d;
best = i;
}
oc += sc1;
}
return best;
}

module.exports = closestCentroid;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 drss = require( '@stdlib/blas/ext/base/drss' ).ndarray;


// MAIN //

/**
* Compute inertia.
* @private
* @param {PositiveInteger} M - number of samples.
* @param {PositiveInteger} N - number of features.
* @param {Float64Array} X - input strided matrix.
* @param {integer} strideX1 - stride length of first dimension of X.
* @param {integer} strideX2 - stride length of second dimension of X.
* @param {integer} offsetX - starting index of X.
* @param {Float64Array} centroids - strided array centroid locations.
* @param {integer} strideC1 - stride length of first dimension of c.
* @param {integer} strideC2 - stride length of second dimension of c.
* @param {integer} offsetC - initial index of centroids.
* @param {Int32Array} labels - labels array.
* @returns {number} inertia.
*/
function computeInertia( M, N, X, strideX1, strideX2, offsetX, centroids, strideC1, strideC2, offsetC, labels ) { // eslint-disable-line max-len, max-params
var inertia;
var ox;
var oc;
var d;
var c;
var i;

inertia = 0.0;
ox = offsetX;
for ( i = 0; i < M; i++ ) {
c = labels[ i ];
oc = offsetC + ( c * strideC1 );
d = drss( N, X, strideX2, ox, centroids, strideC2, oc );
inertia += d;
ox += strideX1;
}
return inertia;
}

module.exports = computeInertia;
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/ml/cluster/strided/dkmeansld/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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';

/**
* Compute fitted cluster results using Lloyd algorithm.
*
* @module @stdlib/ml/cluster/strided/dkmeansld
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
* var kmeans = require( '@stdlib/ml/cluster/strided/dkmeansld' );
*
*/

// MAIN //

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


// EXPORTS //

module.exports = main;
Loading
Loading