forked from sjwhitworth/golearn
/
dbscan.go
188 lines (171 loc) · 4.72 KB
/
dbscan.go
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
184
185
186
187
188
package clustering
import (
"github.com/gonum/matrix/mat64"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/metrics/pairwise"
"math/big"
)
// DBSCANParameters describes the parameters of the density-based
// clustering algorithm DBSCAN
type DBSCANParameters struct {
ClusterParameters
// Eps represents the "reachability", or the maximum
// distance any point can be before being considered for
// inclusion.
Eps float64
// MinCount represents how many points need to be
// in a cluster before it is considered one.
MinCount int
}
func regionQuery(p int, ret *big.Int, dist *mat64.Dense, eps float64) *big.Int {
rows, _ := dist.Dims()
// Return any points within the Eps neighbourhood
for i := 0; i < rows; i++ {
if dist.At(p, i) <= eps {
ret = ret.SetBit(ret, i, 1) // Mark as neighbour
}
}
return ret
}
func computePairwiseDistances(inst base.FixedDataGrid, attrs []base.Attribute, metric pairwise.PairwiseDistanceFunc) (*mat64.Dense, error) {
// Compute pair-wise distances
// First convert everything to floats
mats, err := base.ConvertAllRowsToMat64(attrs, inst)
if err != nil {
return nil, err
}
// Next, do an n^2 computation of all pairwise distances
_, rows := inst.Size()
dist := mat64.NewDense(rows, rows, nil)
for i := 0; i < rows; i++ {
for j := i + 1; j < rows; j++ {
d := metric.Distance(mats[i], mats[j])
dist.Set(i, j, d)
dist.Set(j, i, d)
}
}
return dist, nil
}
// DBSCAN clusters inst using the parameters allowed in and produces a ClusterId->[RowId] map
func DBSCAN(inst base.FixedDataGrid, params DBSCANParameters) (ClusterMap, error) {
// Compute the distances between each possible point
dist, err := computePairwiseDistances(inst, params.Attributes, params.Metric)
if err != nil {
return nil, err
}
_, rows := inst.Size()
clusterMap := make(map[int][]int)
visited := big.NewInt(0)
clustered := big.NewInt(0)
// expandCluster adds P to a cluster C, visiting any neighbours
expandCluster := func(p int, neighbours *big.Int, c int) {
if clustered.Bit(p) == 1 {
panic("Shouldn't happen!")
}
// Add this point to cluster C
if _, ok := clusterMap[c]; !ok {
clusterMap[c] = make([]int, 0)
}
clusterMap[c] = append(clusterMap[c], p)
clustered.SetBit(clustered, p, 1)
visited.SetBit(visited, p, 1)
for i := 0; i < rows; i++ {
reset := false
if neighbours.Bit(i) == 0 {
// Not a neighbour, so skip
continue
}
if visited.Bit(i) == 0 {
// not yet visited
visited = visited.SetBit(visited, i, 1) // Mark as visited
newNeighbours := big.NewInt(0)
newNeighbours = regionQuery(i, newNeighbours, dist, params.Eps)
if BitCount(newNeighbours) >= params.MinCount {
neighbours = neighbours.Or(neighbours, newNeighbours)
reset = true
}
} else {
continue
}
if clustered.Bit(i) == 0 {
clusterMap[c] = append(clusterMap[c], i)
clustered = clustered.SetBit(clustered, i, 1)
}
if reset {
i = 0
}
}
}
c := 0
for i := 0; i < rows; i++ {
if visited.Bit(i) == 1 {
continue // Already visited here
}
visited.SetBit(visited, i, 1)
neighbours := big.NewInt(0)
neighbours = regionQuery(i, neighbours, dist, params.Eps)
if BitCount(neighbours) < params.MinCount {
// Noise, cluster 0
clustered = clustered.Or(clustered, neighbours)
continue
}
c = c + 1 // Increment cluster count
expandCluster(i, neighbours, c)
}
// Remove anything from the map which doesn't make
// minimum points
rmKeys := make([]int, 0)
for id := range clusterMap {
if len(clusterMap[id]) < params.MinCount {
rmKeys = append(rmKeys, id)
}
}
for _, r := range rmKeys {
delete(clusterMap, r)
}
return ClusterMap(clusterMap), nil
}
// How many bits?
func BitCount(n *big.Int) int {
var count int = 0
for _, b := range n.Bytes() {
count += int(bitCounts[b])
}
return count
}
// The bit counts for each byte value (0 - 255).
var bitCounts = []int8{
// Generated by Java BitCount of all values from 0 to 255
0, 1, 1, 2, 1, 2, 2, 3,
1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7,
5, 6, 6, 7, 6, 7, 7, 8,
}