-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffinity_propogation.py
81 lines (69 loc) · 2.72 KB
/
affinity_propogation.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import numpy as np
import scipy
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets import make_blobs
# #############################################################################
# Generate sample data
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(
n_samples=300, centers=centers, cluster_std=0.5, random_state=0
)
# #############################################################################
# Compute Affinity Propagation
# TODO: init preference with median similarity between initial samples
distances = scipy.spatial.distance.cdist(X, X)
# af = AffinityPropagation(preference=np.median(distances), random_state=0).fit(X)
af = AffinityPropagation(preference=-50, random_state=0).fit(X)
# af = AffinityPropagation(random_state=0).fit(X)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
n_clusters_ = len(cluster_centers_indices)
# json.dump({'dvq_checkpoint': 'self.dvq_checkpoint',
# 'labels': af.labels_,
# 'cluster_centers': af.cluster_centers_indices_}, open('/tmp/dvq_compress', 'w'), )
print("Estimated number of clusters: %d" % n_clusters_)
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
print("Adjusted Rand Index: %0.3f" % metrics.adjusted_rand_score(labels_true, labels))
print(
"Adjusted Mutual Information: %0.3f"
% metrics.adjusted_mutual_info_score(labels_true, labels)
)
print(
"Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels, metric="sqeuclidean")
)
print("Calinski Harabasz score: %0.3f",
metrics.calinski_harabasz_score(X, labels)
)
print("Davies Bouldin score: %0.3f",
metrics.davies_bouldin_score(X, labels)
)
# TODO: Jump method if needed https://github.com/v-iashin/JumpMethod/commits?author=v-iashin
# #############################################################################
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle
plt.close("all")
plt.figure(1)
plt.clf()
colors = cycle("bgrcmykbgrcmykbgrcmykbgrcmyk")
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + ".")
plt.plot(
cluster_center[0],
cluster_center[1],
"o",
markerfacecolor=col,
markeredgecolor="k",
markersize=14,
)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
plt.title("Estimated number of clusters: %d" % n_clusters_)
plt.show()