-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathto_sparse_mat.py
59 lines (36 loc) · 1.21 KB
/
to_sparse_mat.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
__author__ = 'quentin'
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import MeanShift, estimate_bandwidth
np.random.seed(seed=5)
arr = np.random.uniform(size=(100,100))
arr = arr > 0.5
#arr[ 10:90,5:70] = 0
xy = np.where(arr)
pts = np.column_stack(xy)
X= pts
bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=100)
bandwidth = 30
ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)
print(("number of estimated clusters : %d" % n_clusters_))
###############################################################################
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle
plt.figure(1)
plt.clf()
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(list(range(n_clusters_)), colors):
my_members = labels == k
cluster_center = cluster_centers[k]
plt.plot(X[my_members, 0], X[my_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()
#