-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
159 lines (134 loc) · 3.59 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
const
Distance = require("./distance.js"),
eudist = Distance.eudist;
/*
DBSCAN(D, epsilon, min_points):
C = 0
for each unvisited point P in dataset
mark P as visited
sphere_points = regionQuery(P, epsilon)
if sizeof(sphere_points) < min_points
ignore P
else
C = next cluster
expandCluster(P, sphere_points, C, epsilon, min_points)
expandCluster(P, sphere_points, C, epsilon, min_points):
add P to cluster C
for each point P’ in sphere_points
if P’ is not visited
mark P’ as visited
sphere_points’ = regionQuery(P’, epsilon)
if sizeof(sphere_points’) >= min_points
sphere_points = sphere_points joined with sphere_points’
if P’ is not yet member of any cluster
add P’ to cluster C
regionQuery(P, epsilon):
return all points within the n-dimensional sphere centered at P with radius epsilon (including P)
*/
class Point {
constructor(v,idx) {
this.v = v;
this.idx = idx || 0;
this.k = -1;
this.visited = false;
}
}
class DBScan {
constructor(data,eps,min) {
this._multi = data[0].length>0;
this._data = this.initData(data);
this._eps = eps;
this._min = min;
}
initData(data) {
let ret = [], len = data.length;
let multi = this._multi;
for(let i=0;i<len;i++) {
ret.push(new Point(multi? data[i] : [data[i]], i));
}
return ret;
}
regionQuery(p) {
let eps = this._eps, data = this._data,
ret = [], len = data.length;
for(let i=0;i<len;i++) {
let np = data[i];
if(np!=p && np.visited) continue;
if(eudist(np.v,p.v,true) < eps)
ret.push(np);
}
return ret;
}
expandCluster(p, region, k) {
let eps = this._eps, data = this._data, min = this._min;
let kdata = k.data, kid = k.id;
// Add p to cluster k
p.k = kid;
kdata.push(p.v);
// region.length is dynamic becaouse items added
// from newRegion to region
for(let j=0;j<region.length;j++) {
// Get a point from the region
let np = region[j];
// If hasn't benn visited
if(!np.visited) {
// Mark as visited
np.visited = true;
// Get the region for this point
let newRegion = this.regionQuery(np), nrlen = newRegion.length;
// If it's a valid region, append to the original region
if(nrlen >= min) {
for(let i=region.length,j=0;j<nrlen;i++,j++)
region[i] = newRegion[j];
}
// if the point isn't assigned to any cluster, assign to current
if(np.k<0) {
np.k = kid;
kdata.push(np.v);
}
}
}
}
dbscan() {
let data = this._data, min = this._min,
len = data.length,
kid = 0,
ks = [], // Clusters
noise = [], // Noise
k = null; // Current cluster
for(let j=0;j<len;j++) {
let p = data[j];
if(!p.visited) {
// Mark as visited
p.visited = true;
// Get the reachable region for this point
let region = this.regionQuery(p);
// Too small region
if(region.length<min) {
noise.push(p.v);
}
// Expand cluster from this region
else {
k = {id:kid++, data:[]};
ks.push(k);
this.expandCluster(p, region, k);
}
}
}
// Restore unidimiensional data that was transformed to
// multidimensional for the algoryth purposes
if(!this._multi) {
ks.forEach(k=>{
k.data = k.data.map(v=>v[0]);
});
noise = noise.map(p=>p[0]);
}
return {
noise : noise,//.map(p=>p.v),
clusters : ks
}
}
}
module.exports = function(data,eps,min) {
return (new DBScan(data,eps,min)).dbscan();
}