Instantly share code, notes, and snippets.

# instance01/predecon.py

Created August 1, 2020 16:02
Show Gist options
• Save instance01/a4f0659a4c951f0996780bca596a3449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # 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[0] - candidate[0]) ** 2) dists_y.append((neighbor[1] - candidate[1]) ** 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[2])) print('p5', is_core(X, X[5]))