-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add ml/base/cluster/kmeans/metrics
#10714
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
Draft
nakul-krishnakumar
wants to merge
7
commits into
stdlib-js:develop
Choose a base branch
from
nakul-krishnakumar:ml-kmeans-result
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.
+738
−0
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41c9793
feat: add `ml/cluster/base/kmeans/metrics`
nakul-krishnakumar a1fa2ce
chore: update copyright years
stdlib-bot f830c7d
fix!: migrate `ml/cluster/base/kmeans` to `ml/base/cluster/kmeans`
nakul-krishnakumar 5e9e1ba
chore: update according to code review
nakul-krishnakumar 8153900
feat: add new `correlation` metric
nakul-krishnakumar 9c85398
remove: remove all wrapper functions
nakul-krishnakumar 21b7c37
chore: update according to code review
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
190 changes: 190 additions & 0 deletions
190
lib/node_modules/@stdlib/ml/base/cluster/kmeans/metrics/README.md
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,190 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # Distance Metrics | ||
|
|
||
| > KMeans distance metrics. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var metrics = require( '@stdlib/ml/base/cluster/kmeans/metrics' ); | ||
| ``` | ||
|
|
||
| #### metrics() | ||
|
|
||
| Returns a list of KMeans distance metrics. | ||
|
|
||
| ```javascript | ||
| var out = metrics(); | ||
| // e.g., returns [ 'sqeuclidean', 'cosine', 'cityblock' , 'correlation' ] | ||
| ``` | ||
|
|
||
| The output array contains the following metrics: | ||
|
|
||
| - `sqeuclidean`: squared euclidean distance. | ||
| - `cosine`: cosine distance. | ||
| - `cityblock`: cityblock (taxicab) distance. | ||
| - `correlation`: correlation distance. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var contains = require( '@stdlib/array/base/assert/contains' ).factory; | ||
| var metrics = require( '@stdlib/ml/base/cluster/kmeans/metrics' ); | ||
|
|
||
| var isMetric = contains( metrics() ); | ||
|
|
||
| var bool = isMetric( 'sqeuclidean' ); | ||
| // returns true | ||
|
|
||
| bool = isMetric( 'cosine' ); | ||
| // returns true | ||
|
|
||
| bool = isMetric( 'beep' ); | ||
| // returns false | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- C interface documentation. --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="c"> | ||
|
|
||
| ## C APIs | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- C usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ### Usage | ||
|
|
||
| ```c | ||
| #include "stdlib/ml/base/cluster/kmeans/metrics.h" | ||
| ``` | ||
|
|
||
| #### STDLIB_ML_CLUSTER_KMEANS_METRIC | ||
|
|
||
| An enumeration of KMeans distance metrics with the following fields: | ||
|
|
||
| - **STDLIB_ML_CLUSTER_KMEANS_SQEUCLIDEAN**: squared euclidean distance. | ||
| - **STDLIB_ML_CLUSTER_KMEANS_COSINE**: cosine distance. | ||
| - **STDLIB_ML_CLUSTER_KMEANS_CITYBLOCK**: cityblock (taxicab) distance. | ||
nakul-krishnakumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - **STDLIB_ML_CLUSTER_KMEANS_CORRELATION**: correlation distance. | ||
|
|
||
| ```c | ||
| #include "stdlib/ml/base/cluster/kmeans/metrics.h" | ||
|
|
||
| const enum STDLIB_ML_CLUSTER_KMEANS_METRIC v = STDLIB_ML_CLUSTER_KMEANS_SQEUCLIDEAN; | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ### Notes | ||
|
|
||
| - Enumeration constants should be considered opaque values, and one should **not** rely on specific integer values. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- C API usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.c --> | ||
|
|
||
| <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/ml/base/cluster/kmeans/metrics/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,48 @@ | ||
| /** | ||
| * @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 isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; | ||
| var pkg = require( './../package.json' ).name; | ||
| var metrics = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var out; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = metrics(); | ||
| if ( out.length < 2 ) { | ||
| b.fail( 'should return an array' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isStringArray( out ) ) { | ||
| b.fail( 'should return an array of strings' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
24 changes: 24 additions & 0 deletions
24
lib/node_modules/@stdlib/ml/base/cluster/kmeans/metrics/docs/repl.txt
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,24 @@ | ||
|
|
||
| {{alias}}() | ||
| Returns a list of distance metrics. | ||
|
|
||
| The output array contains the following metrics: | ||
|
|
||
| - sqeuclidean: squared euclidean distance. | ||
| - cosine: cosine distance. | ||
| - cityblock: cityblock (taxicab) distance. | ||
nakul-krishnakumar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - correlation: correlation distance. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: Array<string> | ||
| List of metrics. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var out = {{alias}}() | ||
| [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ] | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
||
35 changes: 35 additions & 0 deletions
35
lib/node_modules/@stdlib/ml/base/cluster/kmeans/metrics/docs/types/index.d.ts
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,35 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /** | ||
| * Returns a list of distance metrics. | ||
| * | ||
| * @returns list of distance metrics | ||
| * | ||
| * @example | ||
| * var list = metrics(); | ||
| * // e.g., returns [ 'sqeuclidean', 'cosine', 'cityblock', 'correlation' ] | ||
| */ | ||
| declare function metrics(): Array<string>; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = metrics; |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/ml/base/cluster/kmeans/metrics/docs/types/test.ts
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,32 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| import metrics = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns an array of strings... | ||
| { | ||
| metrics(); // $ExpectType string[] | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided any arguments... | ||
| { | ||
| metrics( 9 ); // $ExpectError | ||
| } |
36 changes: 36 additions & 0 deletions
36
lib/node_modules/@stdlib/ml/base/cluster/kmeans/metrics/examples/index.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,36 @@ | ||
| /** | ||
| * @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'; | ||
|
|
||
| var contains = require( '@stdlib/array/base/assert/contains' ).factory; | ||
| var metrics = require( './../lib' ); | ||
|
|
||
| var isMetric = contains( metrics() ); | ||
|
|
||
| var bool = isMetric( 'sqeuclidean' ); | ||
| console.log( bool ); | ||
| // => true | ||
|
|
||
| bool = isMetric( 'cosine' ); | ||
| console.log( bool ); | ||
| // => true | ||
|
|
||
| bool = isMetric( 'beep' ); | ||
| console.log( bool ); | ||
| // => false |
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.
Uh oh!
There was an error while loading. Please reload this page.