All files dkmeans_init_plus_plus.js

98.71% Statements 154/156
92.85% Branches 13/14
100% Functions 1/1
98.71% Lines 154/156

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1572x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 26x 26x 26x 26x 26x 26x 26x 26x 26x 13x 13x 26x     26x 8x 8x 26x 5x 5x 5x 26x 5x 5x 26x 4x 4x 26x 1x 1x 1x 1x 1x 26x 3x 3x 3x 3x 3x 3x 4x 26x 2x 2x 2x 2x 2x  
/**
* @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 isLayout = require( '@stdlib/blas/base/assert/is-layout' );
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' );
var max = require( '@stdlib/math/base/special/fast/max' );
var format = require( '@stdlib/string/format' );
var ndarray = require( './ndarray.js' );
 
 
// MAIN //
 
/**
* Initializes centroids by performing the k-means++ initialization procedure on double-precision floating-point data points.
*
* ## Method
*
* The k-means++ algorithm for choosing initial centroids is as follows:
*
* 1.  Select a data point uniformly at random from a data set \\( X \\). This data point is first centroid and denoted \\( c_0 \\).
*
* 2.  Compute the distance from each data point to \\( c_0 \\). Denote the distance between \\( c_j \\) and data point \\( m \\) as \\( d(x_m, c_j) \\).
*
* 3.  Select the next centroid, \\( c_1 \\), at random from \\( X \\) with probability
*
*     ```tex
*     \frac{d^2(x_m, c_0)}{\sum_{j=0}^{n-1} d^2(x_j, c_0)}
*     ```
*
*     where \\( n \\) is the number of data points.
*
* 4.  To choose centroid \\( j \\),
*
*     a.   Compute the distances from each data point to each centroid and assign each data point to its closest centroid.
*
*     b.   For \\( i = 0,\ldots,n-1 \\) and \\( p = 0,\ldots,j-2 \\), select centroid \\( j \\) at random from \\( X \\) with probability
*
*          ```tex
*          \frac{d^2(x_i, c_p)}{\sum_{\{h; x_h \exits C_p\}} d^2(x_h, c_p)}
*          ```
*
*          where \\( C_p \\) is the set of all data points closest to centroid \\( c_p \\) and \\( x_i \\) belongs to \\( c_p \\).
*
*          Stated more plainly, select each subsequent centroid with a probability proportional to the distance from the centroid to the closest centroid already chosen.
*
* 5.  Repeat step `4` until \\( k \\) centroids have been chosen.
*
* ## References
*
* -   Arthur, David, and Sergei Vassilvitskii. 2007. "K-means++: The Advantages of Careful Seeding." In _Proceedings of the Eighteenth Annual Acm-Siam Symposium on Discrete Algorithms_, 1027–35. SODA '07. Philadelphia, PA, USA: Society for Industrial and Applied Mathematics. <http://dl.acm.org/citation.cfm?id=1283383.1283494>.
*
* @param {string} order - storage layout
* @param {PositiveInteger} k - number of clusters
* @param {PositiveInteger} M - number of data points
* @param {PositiveInteger} N - number of features
* @param {Float64Array} out - input array
* @param {integer} LDO - stride of the first dimension of `out` (a.k.a., leading dimension of the matrix `out`)
* @param {Float64Array} X - input array
* @param {integer} LDX - stride of the first dimension of `x` (a.k.a., leading dimension of the matrix `x`)
* @param {string} metric - distance metric
* @param {PositiveInteger} trials - number of potential centroids per iteration (>= 1)
* @param {*} seed - PRNG seed
* @throws {TypeError} first argument must be a valid order
* @throws {TypeError} tenth argument must be a valid trials (>=1)
* @throws {RangeError} sixth argument must be greater than or equal to max(1,N)
* @throws {RangeError} eighth argument must be greater than or equal to max(1,N)
* @returns {Float64Array} centroids
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var k = 3;
* var M = 5;
* var N = 2;
*
* var out = new Float64Array( k*N );
*
* // Specify data points:
* var xbuf = new Float64Array([
*    0.0, 0.0,
*    1.0, 1.0,
*    1.0, -1.0,
*    -1.0, -1.0,
*    -1.0, 1.0
* ]);
*
* var v = dkmeansInitPlusPlus( 'row-major', k, M, N, out, 2, xbuf, 2, 'sqeuclidean', 3, 44 );
* // returns <Float64Array>[0,0,1,-1,1,1]
*/
function dkmeansInitPlusPlus( order, k, M, N, out, LDO, X, LDX, metric, trials, seed ) { // eslint-disable-line max-len, max-params
	var so1;
	var so2;
	var sx1;
	var sx2;
	var so;
	var sx;
 
	if ( !isLayout( order ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) );
	}
	if ( trials < 1 ) {
		throw new TypeError( format( 'invalid argument. Tenth argument must be a valid trials (>=1). Value: `%s`.', trials ) );
	}
	if ( isRowMajor( order ) ) {
		so = N;
		sx = N;
	} else {
		so = k;
		sx = M;
	}
	if ( LDO < max( 1, so ) ) {
		throw new RangeError( format( 'invalid argument. Fifth argument must be greater than or equal to max(1,%d). Value: `%d`.', so, LDO ) );
	}
	if ( LDX < max( 1, sx ) ) {
		throw new RangeError( format( 'invalid argument. Fifth argument must be greater than or equal to max(1,%d). Value: `%d`.', sx, LDO ) );
	}
	if ( isColumnMajor( order ) ) {
		so1 = 1;
		so2 = LDO;
 
		sx1 = 1;
		sx2 = LDX;
	} else { // order === 'row-major'
		so1 = LDO;
		so2 = 1;
 
		sx1 = LDX;
		sx2 = 1;
	}
	return ndarray( k, M, N, out, so1, so2, 0, X, sx1, sx2, 0, metric, trials, seed ); // eslint-disable-line max-len
}
 
 
// EXPORTS //
 
module.exports = dkmeansInitPlusPlus;