-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
183 lines (162 loc) · 3.71 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*jshint esversion: 6 */
const
Distance = require("./distance.js"),
ClusterInit = require("./kinit.js"),
eudist = Distance.eudist,
mandist = Distance.mandist,
absdist = Distance.dist,
kmrand = ClusterInit.kmrand,
kmpp = ClusterInit.kmpp;
const MAX = 10000;
/**
* Inits an array with values
*/
function init(len,val,v) {
v = v || [];
for(let i=0;i<len;i++) v[i] = val;
return v;
}
function test(point, fndist) {
let
multi = Array.isArray(point),
ks = this.centroids,
k = ks.length;
// For each value in data, find the nearest centroid
let min = Infinity, idx = 0;
for(let j=0;j<k;j++) {
// Custom, Multidimensional or unidimensional
let dist = fndist? fndist(point,ks[j]) :
multi? eudist(point,ks[j]) :
Math.abs(point-ks[j]);
if(dist<=min) {
min = dist;
idx = j;
}
}
return {
idx, centroid:ks[idx]
}
}
function skmeans(data,k,initial,maxit,fndist) {
var ks = [], old = [], idxs = [], dist = [];
var conv = false, it = maxit || MAX;
var len = data.length, vlen = data[0].length, multi = vlen>0;
var count = [];
if(!initial) {
let idxs = {}, z=0;
while(ks.length<k) {
let idx = Math.floor(Math.random()*len);
if(!idxs[idx]) {
idxs[idx] = true;
ks[z++] = data[idx];
}
}
}
else if(initial=="kmrand") {
ks = kmrand(data,k);
}
else if(initial=="kmpp") {
ks = kmpp(data,k,fndist);
}
else {
ks = initial;
}
do {
// Reset k count
init(k,0,count);
// For each value in data, find the nearest centroid
for(let i=0;i<len;i++) {
let min = Infinity, idx = 0;
for(let j=0;j<k;j++) {
// Custom, Multidimensional or unidimensional
var dist = fndist ? fndist(data[i],ks[j]) :
multi? eudist(data[i],ks[j]) :
Math.abs(data[i]-ks[j]);
if(dist<=min) {
min = dist;
idx = j;
}
}
idxs[i] = idx; // Index of the selected centroid for that value
count[idx]++; // Number of values for this centroid
}
// Recalculate centroids
var sum = [], old = [], dif = 0;
if(multi) {
for(let j=0;j<k;j++) {
sum[j] = init(vlen,0,sum[j]);
old[j] = ks[j];
}
}
else {
for(let j=0;j<k;j++) {
sum[j] = 0;
old[j] = ks[j];
}
}
// If multidimensional
if(multi) {
for(let j=0;j<k;j++) ks[j] = [];
// Sum values and count for each centroid
for(let i=0;i<len;i++) {
let idx = idxs[i], // Centroid for that item
vsum = sum[idx], // Sum values for this centroid
vect = data[i]; // Current vector
// Accumulate value on the centroid for current vector
for(let h=0;h<vlen;h++) {
vsum[h] += vect[h];
}
}
// Calculate the average for each centroid
conv = true;
for(let j=0;j<k;j++) {
let ksj = ks[j], // Current centroid
sumj = sum[j], // Accumulated centroid values
oldj = old[j], // Old centroid value
cj = count[j]; // Number of elements for this centroid
// New average
for(let h=0;h<vlen;h++) {
ksj[h] = (sumj[h])/(cj) || 0; // New centroid
}
// Find if centroids have moved
if(conv) {
for(let h=0;h<vlen;h++) {
if(oldj[h]!=ksj[h]) {
conv = false;
break;
}
}
}
}
}
// If unidimensional
else {
// Sum values and count for each centroid
for(let i=0;i<len;i++) {
let idx = idxs[i];
sum[idx] += data[i];
}
// Calculate the average for each centroid
for(let j=0;j<k;j++) {
ks[j] = sum[j]/count[j] || 0; // New centroid
}
// Find if centroids have moved
conv = true;
for(let j=0;j<k;j++) {
if(old[j]!=ks[j]) {
conv = false;
break;
}
}
}
conv = conv || (--it<=0);
}while(!conv);
return {
it : (maxit || MAX) - it,
k : k,
idxs : idxs,
centroids : ks,
test : test
};
}
module.exports = skmeans;