I have been searching around for an implementation of DBSCAN for 3 dimensional points without much luck. Does anyone know I library that handles this or has any experience with doing this? I am assuming that the DBSCAN algorithm can handle 3 dimensions, by having the e value be a radius metric and the distance between points measured by euclidean separation. If anyone has tried implementing this and would like to share that would also be greatly appreciated, thanks.
Advertisement
Answer
So this is what I came up with, I know it is not the most efficient implementation but it works; for example the region query, which is the main time eater of the algorithm computes the distance between two points more than once, instead of just storing it for use later.
JavaScript
x
60
60
1
class DBSCAN(object):
2
3
def __init__(self, eps=0, min_points=2):
4
self.eps = eps
5
self.min_points = min_points
6
self.visited = []
7
self.noise = []
8
self.clusters = []
9
self.dp = []
10
11
def cluster(self, data_points):
12
self.visited = []
13
self.dp = data_points
14
c = 0
15
for point in data_points:
16
if point not in self.visited:
17
self.visited.append(point)
18
neighbours = self.region_query(point)
19
if len(neighbours) < self.min_points:
20
self.noise.append(point)
21
else:
22
c += 1
23
self.expand_cluster(c, neighbours)
24
25
def expand_cluster(self, cluster_number, p_neighbours):
26
cluster = ("Cluster: %d" % cluster_number, [])
27
self.clusters.append(cluster)
28
new_points = p_neighbours
29
while new_points:
30
new_points = self.pool(cluster, new_points)
31
32
def region_query(self, p):
33
result = []
34
for d in self.dp:
35
distance = (((d[0] - p[0])**2 + (d[1] - p[1])**2 + (d[2] - p[2])**2)**0.5)
36
if distance <= self.eps:
37
result.append(d)
38
return result
39
40
def pool(self, cluster, p_neighbours):
41
new_neighbours = []
42
for n in p_neighbours:
43
if n not in self.visited:
44
self.visited.append(n)
45
n_neighbours = self.region_query(n)
46
if len(n_neighbours) >= self.min_points:
47
new_neighbours = self.unexplored(p_neighbours, n_neighbours)
48
for c in self.clusters:
49
if n not in c[1] and n not in cluster[1]:
50
cluster[1].append(n)
51
return new_neighbours
52
53
@staticmethod
54
def unexplored(x, y):
55
z = []
56
for p in y:
57
if p not in x:
58
z.append(p)
59
return z
60