deno.land / x / simplestatistic@v7.7.1 / src / k_means_cluster.js

k_means_cluster.js
نووسراو ببینە
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
import euclideanDistance from "./euclidean_distance";import makeMatrix from "./make_matrix";import sample from "./sample";
/** * @typedef {Object} kMeansReturn * @property {Array<number>} labels The labels. * @property {Array<Array<number>>} centroids The cluster centroids. */
/** * Perform k-means clustering. * * @param {Array<Array<number>>} points N-dimensional coordinates of points to be clustered. * @param {number} numCluster How many clusters to create. * @param {Function} randomSource An optional entropy source that generates uniform values in [0, 1). * @return {kMeansReturn} Labels (same length as data) and centroids (same length as numCluster). * @throws {Error} If any centroids wind up friendless (i.e., without associated points). * * @example * kMeansCluster([[0.0, 0.5], [1.0, 0.5]], 2); // => {labels: [0, 1], centroids: [[0.0, 0.5], [1.0 0.5]]} */function kMeansCluster(points, numCluster, randomSource = Math.random) { let oldCentroids = null; let newCentroids = sample(points, numCluster, randomSource); let labels = null; let change = Number.MAX_VALUE; while (change !== 0) { labels = labelPoints(points, newCentroids); oldCentroids = newCentroids; newCentroids = calculateCentroids(points, labels, numCluster); change = calculateChange(newCentroids, oldCentroids); } return { labels: labels, centroids: newCentroids };}
/** * Label each point according to which centroid it is closest to. * * @private * @param {Array<Array<number>>} points Array of XY coordinates. * @param {Array<Array<number>>} centroids Current centroids. * @return {Array<number>} Group labels. */function labelPoints(points, centroids) { return points.map((p) => { let minDist = Number.MAX_VALUE; let label = -1; for (let i = 0; i < centroids.length; i++) { const dist = euclideanDistance(p, centroids[i]); if (dist < minDist) { minDist = dist; label = i; } } return label; });}
/** * Calculate centroids for points given labels. * * @private * @param {Array<Array<number>>} points Array of XY coordinates. * @param {Array<number>} labels Which groups points belong to. * @param {number} numCluster Number of clusters being created. * @return {Array<Array<number>>} Centroid for each group. * @throws {Error} If any centroids wind up friendless (i.e., without associated points). */function calculateCentroids(points, labels, numCluster) { // Initialize accumulators. const dimension = points[0].length; const centroids = makeMatrix(numCluster, dimension); const counts = Array(numCluster).fill(0);
// Add points to centroids' accumulators and count points per centroid. const numPoints = points.length; for (let i = 0; i < numPoints; i++) { const point = points[i]; const label = labels[i]; const current = centroids[label]; for (let j = 0; j < dimension; j++) { current[j] += point[j]; } counts[label] += 1; }
// Rescale centroids, checking for any that have no points. for (let i = 0; i < numCluster; i++) { if (counts[i] === 0) { throw new Error(`Centroid ${i} has no friends`); } const centroid = centroids[i]; for (let j = 0; j < dimension; j++) { centroid[j] /= counts[i]; } }
return centroids;}
/** * Calculate the difference between old centroids and new centroids. * * @private * @param {Array<Array<number>>} left One list of centroids. * @param {Array<Array<number>>} right Another list of centroids. * @return {number} Distance between centroids. */function calculateChange(left, right) { let total = 0; for (let i = 0; i < left.length; i++) { total += euclideanDistance(left[i], right[i]); } return total;}
export default kMeansCluster;
simplestatistic

Version Info

Tagged at
2 years ago