-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (45 loc) · 1.22 KB
/
index.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
'use strict';
var cluster = require('./lib/cluster.js'),
metrics = require('./lib/metrics.js');
module.exports = function (inputs, options) {
var key, defaults = {
maxLinkage: Infinity,
distance: metrics.distance.manhattan,
linkage: metrics.linkage.average
}, levels;
if (options) {
for (key in options) {
if (typeof options[key] === 'string') {
defaults[key] = metrics[key][options[key]];
} else {
defaults[key] = options[key];
}
}
}
levels = cluster(inputs, defaults);
levels.fit = function (regression, callback) {
var length = levels.length,
index = 0,
points = [],
x, y, line;
for (; index < length; index++) {
x = (index + 1) / length;
y = levels[index].linkage;
if (callback !== undefined &&
callback(x, y) === false) {
continue;
}
points.push([x, y]);
}
line = regression(points);
line.points.forEach(function (point, i) {
point.index = i;
point.level = levels[i];
point.point = points[i];
point.linkage = point.level.linkage;
point.distance = distance(point, points[i]);
});
return line;
}
return levels;
}