@thi.ng/k-means 0.6.95 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-07-22T13:15:57Z
3
+ - **Last updated**: 2024-08-10T15:03:07Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,15 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/k-means@0.7.0) (2024-07-25)
13
+
14
+ #### 🚀 Features
15
+
16
+ - update `KMeansOpts.initial` ([f70073d](https://github.com/thi-ng/umbrella/commit/f70073d))
17
+ - support custom init functions
18
+ - add KMeansInit type alias
19
+ - update handling in `kmeans()`
20
+
12
21
  ### [0.6.89](https://github.com/thi-ng/umbrella/tree/@thi.ng/k-means@0.6.89) (2024-06-21)
13
22
 
14
23
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -59,7 +59,7 @@ For Node.js REPL:
59
59
  const kmeans = await import("@thi.ng/k-means");
60
60
  ```
61
61
 
62
- Package sizes (brotli'd, pre-treeshake): ESM: 870 bytes
62
+ Package sizes (brotli'd, pre-treeshake): ESM: 884 bytes
63
63
 
64
64
  ## Dependencies
65
65
 
package/api.d.ts CHANGED
@@ -5,9 +5,10 @@ import type { ReadonlyVec, Vec } from "@thi.ng/vectors";
5
5
  export interface KMeansOpts {
6
6
  /**
7
7
  * Array of initial centroids (i.e. indices of selected points in `samples`
8
- * array given to {@link kmeans}).
8
+ * array given to {@link kmeans}) or a function producing such. If omitted,
9
+ * {@link initKmeanspp} is used as default.
9
10
  */
10
- initial: number[];
11
+ initial: number[] | KMeansInit<ReadonlyVec>;
11
12
  /**
12
13
  * Distance function/metric to use for finding nearest centroid.
13
14
  */
@@ -25,6 +26,10 @@ export interface KMeansOpts {
25
26
  */
26
27
  strategy: CentroidStrategy;
27
28
  }
29
+ /**
30
+ * k-means initialization function, e.g. {@link initKmeanspp}.
31
+ */
32
+ export type KMeansInit<T extends ReadonlyVec> = (k: number, samples: T[], dist?: IDistance<ReadonlyVec>, rnd?: IRandom) => number[];
28
33
  export type CentroidStrategy = Fn<number, {
29
34
  update: Fn<ReadonlyVec, void>;
30
35
  finish: Fn0<Maybe<Vec>>;
package/kmeans.js CHANGED
@@ -16,7 +16,7 @@ const kmeans = (k, samples, opts) => {
16
16
  };
17
17
  const num = samples.length;
18
18
  const dim = samples[0].length;
19
- const centroidIDs = initial || initKmeanspp(k, samples, dist, rnd);
19
+ const centroidIDs = Array.isArray(initial) ? initial : initial ? initial(k, samples, dist, rnd) : initKmeanspp(k, samples, dist, rnd);
20
20
  assert(centroidIDs.length > 0, `missing initial centroids`);
21
21
  k = centroidIDs.length;
22
22
  const centroids = centroidIDs.map((i) => samples[i]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/k-means",
3
- "version": "0.6.95",
3
+ "version": "0.7.1",
4
4
  "description": "Configurable k-means & k-medians (with k-means++ initialization) for n-D vectors",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -36,25 +36,26 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.11.7",
40
- "@thi.ng/distance": "^2.4.80",
41
- "@thi.ng/errors": "^2.5.13",
42
- "@thi.ng/random": "^4.0.1",
43
- "@thi.ng/vectors": "^7.11.6"
39
+ "@thi.ng/api": "^8.11.8",
40
+ "@thi.ng/distance": "^2.4.81",
41
+ "@thi.ng/errors": "^2.5.14",
42
+ "@thi.ng/random": "^4.0.2",
43
+ "@thi.ng/vectors": "^7.11.7"
44
44
  },
45
45
  "devDependencies": {
46
- "@microsoft/api-extractor": "^7.47.0",
46
+ "@microsoft/api-extractor": "^7.47.5",
47
47
  "esbuild": "^0.23.0",
48
- "typedoc": "^0.26.3",
49
- "typescript": "^5.5.3"
48
+ "typedoc": "^0.26.5",
49
+ "typescript": "^5.5.4"
50
50
  },
51
51
  "keywords": [
52
52
  "cluster",
53
53
  "distance",
54
- "k-medians",
55
54
  "k-means",
56
55
  "k-means++",
56
+ "k-medians",
57
57
  "nd",
58
+ "self-organizing",
58
59
  "typescript"
59
60
  ],
60
61
  "publishConfig": {
@@ -81,7 +82,8 @@
81
82
  "thi.ng": {
82
83
  "alias": "kmeans",
83
84
  "status": "beta",
84
- "year": 2021
85
+ "year": 2021,
86
+ "screenshot": "examples/kmeans-viz.jpg"
85
87
  },
86
- "gitHead": "bd22b0826134b79064169371665b4d6caa9b6066\n"
88
+ "gitHead": "ec78f98d015e4d214a0b840e72e497407807daf3\n"
87
89
  }