-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add ml/strided/dkmeans-init-plus-plus
#12312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nakul-krishnakumar
wants to merge
15
commits into
stdlib-js:develop
Choose a base branch
from
nakul-krishnakumar:ml-strided-dkmeanspp
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,545
−0
Open
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ade68cf
feat: add `ml/strided/dkmeans-init-plus-plus`
nakul-krishnakumar 910e515
fix: add argument validation for `trials`
nakul-krishnakumar 4d8fd87
test: add tests for `ml/strided/dkmeans-init-plus-plus
nakul-krishnakumar e45a3aa
bench: add JS benchmarks for `ml/strided/dkmeans-init-plus-plus
nakul-krishnakumar b75757a
Merge remote-tracking branch 'upstream/develop' into ml-strided-dkmea…
stdlib-bot 7c045c1
fix: add early return for invalid args
nakul-krishnakumar d1ffd9b
Merge remote-tracking branch 'upstream/develop' into ml-strided-dkmea…
stdlib-bot 78ebd7b
refactor: update according to code review
nakul-krishnakumar e348fd7
feat: add `ml/strided/dkmeans-init-plus-plus`
nakul-krishnakumar 95ceb99
chore: update according to code review
nakul-krishnakumar e08a382
test: remove unwanted tests according to lint error
nakul-krishnakumar ac0d951
refactor: add debug logs and fix bug
nakul-krishnakumar aeb3cf9
chore: update according to code review
nakul-krishnakumar 828a911
chore: update according to code review
nakul-krishnakumar 8786fad
test: updated tests to be more deterministic
nakul-krishnakumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
lib/node_modules/@stdlib/ml/strided/dkmeans-init-plus-plus/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /** | ||
| * @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 bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var dkmeansInitPlusPlus = require( './../lib/dkmeans_init_plus_plus.js' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var LAYOUTS = [ | ||
| 'row-major', | ||
| 'column-major' | ||
| ]; | ||
|
|
||
| var options = { | ||
| 'dtype': 'float64' | ||
| }; | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @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 {string} metric - distance metric | ||
| * @param {PositiveInteger} trials - number of potential centroids per iteration (>= 1) | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( order, k, M, N, metric, trials ) { | ||
| var out = new Float64Array( k*N ); | ||
| var x = uniform( M*N, -100.0, 100.0, options ); | ||
| return benchmark; | ||
|
|
||
| /** | ||
| * Benchmark function. | ||
| * | ||
| * @private | ||
| * @param {Benchmark} b - benchmark instance | ||
| */ | ||
| function benchmark( b ) { | ||
| var c; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| c = dkmeansInitPlusPlus( order, k, M, N, out, N, x, N, metric, trials, 44 ); // eslint-disable-line max-len | ||
| if ( isnan( c[ i%(k*N) ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( c[ 0 ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Main execution sequence. | ||
| * | ||
| * @private | ||
| */ | ||
| function main() { | ||
| var metrics; | ||
| var trials; | ||
| var min; | ||
| var max; | ||
| var M; | ||
| var N; | ||
| var k; | ||
| var t; | ||
| var m; | ||
| var i; | ||
| var j; | ||
| var f; | ||
|
|
||
| min = 1; // 10^min | ||
| max = 4; // 10^max | ||
|
|
||
| // Benchmark: vary order with defaults k=2, M=100, N=2, metric=sqeuclidean, trials=1... | ||
| for ( i = 0; i <= LAYOUTS.length; i++ ) { | ||
| f = createBenchmark( LAYOUTS[ i ], 2, 100, 2, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_M:order=%s,k=2,M=100,N=2,metric=sqeuclidean,trials=1', pkg, LAYOUTS[ i ] ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of data points (M) with defaults k=2, N=2, metric=sqeuclidean, trials=1... | ||
| for ( i = min; i <= max; i++ ) { | ||
| M = pow( 10, i ); | ||
| f = createBenchmark( 'row-major', 2, M, 2, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_M:order=row-major,k=2,M=%d,N=2,metric=sqeuclidean,trials=1', pkg, M ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of features (N) with defaults k=2, M=10, metric=sqeuclidean, trials=1... | ||
| for ( i = min; i <= max; i++ ) { | ||
| N = pow( 10, i ); | ||
| f = createBenchmark( 'row-major', 2, 10, N, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_N:order=row-major,k=2,M=1000,N=%d,metric=sqeuclidean,trials=1', pkg, N ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of clusters (k) with defaults M=1000, N=2, metric=sqeuclidean, trials=1... | ||
| for ( i = 1; i <= 4; i++ ) { | ||
| k = pow( 2, i ); | ||
| f = createBenchmark( 'row-major', k, 1000, 2, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_k:order=row-major,k=%d,M=1000,N=2,metric=sqeuclidean,trials=1', pkg, k ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of trials with defaults k=2, M=1000, N=2, metric=sqeuclidean... | ||
| trials = [ 1, 10, 100, 1000 ]; | ||
| for ( j = 0; j < trials.length; j++ ) { | ||
| t = trials[ j ]; | ||
| f = createBenchmark( 'row-major', 2, 1000, 2, 'sqeuclidean', t ); | ||
| bench( format( '%s::vary_trials:order=row-major,k=2,M=1000,N=2,metric=sqeuclidean,trials=%d', pkg, t ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary metric with defaults k=2, M=1000, N=2, trials=1... | ||
| metrics = [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ]; | ||
| for ( j = 0; j < metrics.length; j++ ) { | ||
| m = metrics[ j ]; | ||
| f = createBenchmark( 'row-major', 2, 1000, 2, m, 1 ); | ||
| bench( format( '%s::vary_metric:order=row-major,k=2,M=1000,N=2,metric=%s,trials=1', pkg, m ), f ); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
147 changes: 147 additions & 0 deletions
147
lib/node_modules/@stdlib/ml/strided/dkmeans-init-plus-plus/benchmark/benchmark.ndarray.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * @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 bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var dkmeansInitPlusPlus = require( './../lib/ndarray.js' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var options = { | ||
| 'dtype': 'float64' | ||
| }; | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @param {PositiveInteger} k - number of clusters | ||
| * @param {PositiveInteger} M - number of data points | ||
| * @param {PositiveInteger} N - number of features | ||
| * @param {string} metric - distance metric | ||
| * @param {PositiveInteger} trials - number of potential centroids per iteration (>= 1) | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( k, M, N, metric, trials ) { | ||
| var out = new Float64Array( k*N ); | ||
| var x = uniform( M*N, -100.0, 100.0, options ); | ||
| return benchmark; | ||
|
|
||
| /** | ||
| * Benchmark function. | ||
| * | ||
| * @private | ||
| * @param {Benchmark} b - benchmark instance | ||
| */ | ||
| function benchmark( b ) { | ||
| var c; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| c = dkmeansInitPlusPlus( k, M, N, out, N, 1, 0, x, N, 1, 0, metric, trials, 44 ); // eslint-disable-line max-len | ||
| if ( isnan( c[ i%(k*N) ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( c[ 0 ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Main execution sequence. | ||
| * | ||
| * @private | ||
| */ | ||
| function main() { | ||
| var metrics; | ||
| var trials; | ||
| var min; | ||
| var max; | ||
| var M; | ||
| var N; | ||
| var k; | ||
| var t; | ||
| var m; | ||
| var i; | ||
| var j; | ||
| var f; | ||
|
|
||
| min = 1; // 10^min | ||
| max = 4; // 10^max | ||
|
|
||
| // Benchmark: vary number of data points (M) with defaults k=2, N=2, metric=sqeuclidean, trials=1... | ||
| for ( i = min; i <= max; i++ ) { | ||
| M = pow( 10, i ); | ||
| f = createBenchmark( 2, M, 2, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_M:k=2,M=%d,N=2,metric=sqeuclidean,trials=1', pkg, M ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of features (N) with defaults k=2, M=10, metric=sqeuclidean, trials=1... | ||
| for ( i = min; i <= max; i++ ) { | ||
| N = pow( 10, i ); | ||
| f = createBenchmark( 2, 10, N, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_N:k=2,M=1000,N=%d,metric=sqeuclidean,trials=1', pkg, N ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of clusters (k) with defaults M=1000, N=2, metric=sqeuclidean, trials=1... | ||
| for ( i = 1; i <= 4; i++ ) { | ||
| k = pow( 2, i ); | ||
| f = createBenchmark( k, 1000, 2, 'sqeuclidean', 1 ); | ||
| bench( format( '%s::vary_k:k=%d,M=1000,N=2,metric=sqeuclidean,trials=1', pkg, k ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary number of trials with defaults k=2, M=1000, N=2, metric=sqeuclidean... | ||
| trials = [ 1, 10, 100 ]; | ||
| for ( j = 0; j < trials.length; j++ ) { | ||
| t = trials[ j ]; | ||
| f = createBenchmark( 2, 1000, 2, 'sqeuclidean', t ); | ||
| bench( format( '%s::vary_trials:k=2,M=1000,N=2,metric=sqeuclidean,trials=%d', pkg, t ), f ); | ||
| } | ||
|
|
||
| // Benchmark: vary metric with defaults k=2, M=1000, N=2, trials=1... | ||
| metrics = [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ]; | ||
| for ( j = 0; j < metrics.length; j++ ) { | ||
| m = metrics[ j ]; | ||
| f = createBenchmark( 2, 1000, 2, m, 1 ); | ||
| bench( format( '%s::vary_metric:k=2,M=1000,N=2,metric=%s,trials=1', pkg, m ), f ); | ||
| } | ||
| } | ||
|
|
||
| main(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have to reset the workspace array?