Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# instance01/predecon.py

Created Aug 1, 2020
 # PreDeCon - Projected Clustering # Paper: Density Connected Clustering with Local Subspace Preferences, Böhm et al., 2004 # This code is not optimized. import numpy as np def get_neighbor(X, candidate): """Return the eps-neighbors of candidate. """ neighbors = [] for pt in X: if ((pt - candidate) ** 2).sum() ** .5 <= eps: neighbors.append(pt) return neighbors def get_weights(X, candidate): dists_x = [] dists_y = [] for neighbor in get_neighbor(X, candidate): dists_x.append((neighbor - candidate) ** 2) dists_y.append((neighbor - candidate) ** 2) var_x = sum(dists_x) / len(dists_x) var_y = sum(dists_y) / len(dists_y) weight_x = 1 if var_x > delta else K weight_y = 1 if var_y > delta else K return weight_x, weight_y def pref_weighted_dist(X, neighbor, candidate): weights = get_weights(X, neighbor) dist = 0 for i in range(2): dist += weights[i] * (neighbor[i] - candidate[i]) ** 2 return dist ** .5 def is_core(X, candidate): good_ones = [] for neighbor in get_neighbor(X, candidate): dist = max( pref_weighted_dist(X, neighbor, candidate), pref_weighted_dist(X, candidate, neighbor) ) if dist <= eps: good_ones.append(dist) return len(good_ones) >= minpts X = np.array([ [1, 5], [2, 5], [3, 5], # p3 [4, 5], [5, 5], [6, 5], # p6 [7, 5], [7, 6], [7, 7], [7, 4], [7, 3], [7, 2] ]) minpts = 3 eps = 1 delta = 0.25 K = 100 print('p3', is_core(X, X)) print('p5', is_core(X, X))

### instance01 commented Aug 2, 2020

 Accompanying blog post: https://blog.xa0.de/post/PreDeCon%20---%20Density-based-Projected-Subspace-Clustering/
to join this conversation on GitHub. Already have an account? Sign in to comment