def dbscan(eps, minpts, point_list): counter = MeowCounter() cluster_list = [] for p in point_list: if p.label is None: p.label = "visited" counter.count() n_eps = find_n(p, eps, point_list) if len(n_eps) < minpts: p.label = "noise" else: cluster = Cluster() cluster_list.append(cluster) cluster.add_point(p) while len(n_eps) > 0: p_p = n_eps.pop() if p_p.label is None: p_p.label = "visited" counter.count() n_eps_2 = find_n(p_p, eps, point_list) if len(n_eps_2) >= minpts: for i in n_eps_2: if i.label is None: n_eps.append(i) if p_p.cluster is None: cluster.add_point(p_p) return cluster_list
def dbscan(self, data): self.init_params() self.data = data ## Setting up the plot fig = plt.figure() axis_proj = 'rectilinear' if self.dim > 2: axis_proj = '%dd' % self.dim ax = fig.add_subplot(111, projection = axis_proj) #default noise cluster noise = Cluster('Noise', self.dim) self.clusters.add(noise) for point in data: if point not in self.visited: self.visited.append(point) neighbour_pts = self.region_query(point) if len(neighbour_pts) < self.min_pts: noise.add_point(point) else: name = 'cluster-%d' % self.cluster_count new_cluster = Cluster(name, self.dim) self.cluster_count += 1 self.expand_cluster(new_cluster, point, neighbour_pts) if self.dim == 2: ax.scatter(new_cluster.get_X(), new_cluster.get_Y(), c = self.color[self.cluster_count % len(self.color)], marker = 'o', label = name) elif self.dim == 3: ax.scatter(new_cluster.get_X(), new_cluster.get_Y(), new_cluster.get_Z(), marker = 'o', c = self.color[self.cluster_count % len(self.color)], label = name) ax.hold(True) if len(noise.get_points()) != 0: if self.dim > 2: ax.scatter(noise.get_X(), noise.get_Y(), noise.get_Z(), marker = 'x', label = noise.name) else: ax.scatter(noise.get_X(), noise.get_Y(), marker = 'x', label = noise.name) print ("Number of clusters found: %d" % self.cluster_count) ax.hold(False) ax.legend(loc='lower left') ax.grid(True) plt.title(r'DBSCAN Clustering', fontsize=18) plt.show()
def dbscan(self, data): self.init_params() self.data = data ## Setting up the plot # fig = plt.figure() axis_proj = 'rectilinear' if self.dim > 2: axis_proj = '%dd' % self.dim # ax = fig.add_subplot(111, projection = axis_proj) # default noise cluster noise = Cluster('Noise', self.dim) self.clusters.add(noise) for point in data: if point not in self.visited: self.visited.append(point) neighbour_pts = self.region_query(point) if len(neighbour_pts) < self.min_pts: noise.add_point(point) else: name = 'cluster-%d' % self.cluster_count new_cluster = Cluster(name, self.dim) self.cluster_count += 1 self.expand_cluster(new_cluster, point, neighbour_pts) # if self.dim == 2: # ax.scatter(new_cluster.get_X(), new_cluster.get_Y(), c = self.color[self.cluster_count % len(self.color)], # marker = 'o', label = name) # elif self.dim == 3: # ax.scatter(new_cluster.get_X(), new_cluster.get_Y(), new_cluster.get_Z(), marker = 'o', # c = self.color[self.cluster_count % len(self.color)], label = name) # if len(noise.get_points()) != 0: # if self.dim > 2: # ax.scatter(noise.get_X(), noise.get_Y(), noise.get_Z(), marker = 'x', label = noise.name) # else: # ax.scatter(noise.get_X(), noise.get_Y(), marker = 'x', label = noise.name) print("Number of clusters found: %d" % self.cluster_count)
def dbscan(self, data): self.init_params() self.data = data ## Setting up the plot fig = plt.figure() axis_proj = 'rectilinear' if self.dim > 2: axis_proj = '%dd' % self.dim ax = fig.add_subplot(111, projection=axis_proj) #default noise cluster noise = Cluster('Noise', self.dim) self.clusters.add(noise) for point in data: if point not in self.visited: self.visited.append(point) neighbour_pts = self.region_query(point) if len(neighbour_pts) < self.min_pts: noise.add_point(point) else: name = 'cluster-%d' % self.cluster_count new_cluster = Cluster(name, self.dim) self.cluster_count += 1 self.expand_cluster(new_cluster, point, neighbour_pts) if self.dim == 2: ax.scatter(new_cluster.get_X(), new_cluster.get_Y(), c=self.color[self.cluster_count % len(self.color)], marker='o', label=name) elif self.dim == 3: ax.scatter(new_cluster.get_X(), new_cluster.get_Y(), new_cluster.get_Z(), marker='o', c=self.color[self.cluster_count % len(self.color)], label=name) ax.hold(True) if len(noise.get_points()) != 0: if self.dim > 2: ax.scatter(noise.get_X(), noise.get_Y(), noise.get_Z(), marker='x', label=noise.name) else: ax.scatter(noise.get_X(), noise.get_Y(), marker='x', label=noise.name) print("Number of clusters found: %d" % self.cluster_count) ax.hold(False) ax.legend(loc='lower left') ax.grid(True) plt.title(r'DBSCAN Clustering', fontsize=18) plt.show()