-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
78 lines (57 loc) · 1.95 KB
/
test.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from sklearn.cluster import SpectralClustering
from sklearn.datasets import make_blobs
from sklearn.neighbors import kneighbors_graph
from autosp import predict_k
def consistent_labels(labels):
"""Achieve "some" consistency of color between true labels and pred labels.
Parameters
----------
labels : array of integers, shape: n_samples
The labels of the clusters.
Returns
----------
color_map : dict object {integer: integer}
The map of labels.
"""
color_map = {}
i = 0
v = 0
while v != max(labels) + 1:
if labels[i] in color_map:
pass
else:
color_map[labels[i]] = v
v += 1
i += 1
return color_map
if __name__ == "__main__":
# Generate artificial datasets.
number_of_blobs = 5 # You can change this!!
data, labels_true = make_blobs(n_samples=number_of_blobs * 10,
centers=number_of_blobs)
# Calculate affinity_matrix.
connectivity = kneighbors_graph(data, n_neighbors=10)
affinity_matrix = 0.5 * (connectivity + connectivity.T)
# auto_spectral_clustering
k = predict_k(affinity_matrix)
sc = SpectralClustering(n_clusters=k,
affinity="precomputed",
assign_labels="kmeans").fit(affinity_matrix)
labels_pred = sc.labels_
print("%d blobs(artificial datasets)." % number_of_blobs)
print("%d clusters(predicted)." % k)
# Plot.
from pylab import *
t_map = consistent_labels(labels_true)
t = [t_map[v] for v in labels_true]
p_map = consistent_labels(labels_pred)
p = [p_map[v] for v in labels_pred]
subplot(211)
title("%d blobs(artificial datasets)." % number_of_blobs)
scatter(data[:, 0], data[:, 1], s=150, c=t)
subplot(212)
title("%d clusters(predicted)." % k)
scatter(data[:, 0], data[:, 1], s=150, c=p)
show()