Add typing for "skmeans" (#43059)

Co-authored-by: Adam Zerella <adamzerella@users.noreply.github.com>
This commit is contained in:
Adam Zerella
2020-03-27 13:13:08 -04:00
committed by GitHub
co-authored by Adam Zerella
parent 234b6171fc
commit 5ed7a06ce8
4 changed files with 82 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
// Type definitions for skmeans 0.11
// Project: https://github.com/solzimer/skmeans
// Definitions by: Adam Zerella <https://github.com/adamzerella>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type CentroidValues = 'kmrand' | 'kmpp' | null;
interface Data {
it: number;
k: number;
centroids: number;
idxs: number[];
test: (x: number, point?: (x1: number, x2: number) => number) => void;
}
/**
* Calculates unidimiensional and multidimensional k-means clustering on data.
*
* @param data Unidimiensional or multidimensional array of values to be clustered.
* @param k Number of clusters.
* @param centroids Initial centroid values.
* @param iterations Maximum number of iterations. If not provided, it will be set to 10000.
* @param distance Custom distance function. Takes two points as arguments and returns a scalar number.
*/
declare function skmeans(
data: number[] | number[][],
k: number,
centroids?: CentroidValues,
iterations?: number | null,
distance?: (x: number, y: number) => number
): Data;
export = skmeans;
+21
View File
@@ -0,0 +1,21 @@
import skmeans = require('skmeans');
const dataUni = [0, 1, 3, 6, 8, 3];
const dataMulti = [[0, 3, 4], [5, 4, 9]];
skmeans(dataUni, 4, "kmrand");
skmeans(dataUni, 3, "kmpp");
skmeans(dataMulti, 6);
skmeans(dataMulti, 3, null, 10);
skmeans(dataMulti, 3, null, null, (x1, x2) => Math.abs(x1 - x2));
const res = skmeans(dataMulti, 3, null, 10);
res.test(6);
const res2 = skmeans(dataMulti, 3, null, 10);
res2.test(6, (x1, x2) => Math.abs(x1 - x2));
+25
View File
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"skmeans-tests.ts"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}