This repository has been archived by the owner on Jan 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigenvector_based.py
51 lines (40 loc) · 1.64 KB
/
eigenvector_based.py
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
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import pairwise_distances
from utils import gaussian_kernel, plot_clustering_result,generate_dataset
def eigenvector_thresholding(evect, threshold, clustered_elements):
clustered_idx = []
new_clustered_elements = clustered_elements.copy()
for i in range(len(evect)):
if i not in clustered_elements and abs(evect[i]) > threshold:
clustered_idx.append(i)
new_clustered_elements.append(i)
return clustered_idx, new_clustered_elements
def apply_eigenvector_based(X, A, threshold=0.001):
"""
Apply k-means to set of points X, according to input parameters.
Args:
X: list of 2d points
K: number of cluster to find
threshold: value for thresholding the characteristic vector
Returns:
clusters: vector indicating cluster for each point in X
noise: vector of points not clustered
"""
evals, evects = np.linalg.eig(A)
evects = evects.T # from column-eigenvectors to row-eigenvectors
clusters = []
noise = []
clustered_elements = []
continue_clustering = True
while(continue_clustering):
if np.amax(evals) == -float("inf"):
break
bigger_eig_id = np.argmax(evals)
cluster, clustered_elements = eigenvector_thresholding(evects[bigger_eig_id],
threshold, clustered_elements)
clusters.append(cluster)
if len(clustered_elements) == len(X):
continue_clustering = False
evals[bigger_eig_id] = -float("inf")
return clusters, noise