|
22 | 22 | # ############################################################################# |
23 | 23 | # Generate sample data |
24 | 24 | centers = [[1, 1], [-1, -1], [1, -1]] |
25 | | -X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4, |
26 | | - random_state=0) |
| 25 | +X, labels_true = make_blobs( |
| 26 | + n_samples=750, centers=centers, cluster_std=0.4, random_state=0 |
| 27 | +) |
27 | 28 |
|
28 | 29 | X = StandardScaler().fit_transform(X) |
29 | 30 |
|
|
38 | 39 | n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) |
39 | 40 | n_noise_ = list(labels).count(-1) |
40 | 41 |
|
41 | | -print('Estimated number of clusters: %d' % n_clusters_) |
42 | | -print('Estimated number of noise points: %d' % n_noise_) |
| 42 | +print("Estimated number of clusters: %d" % n_clusters_) |
| 43 | +print("Estimated number of noise points: %d" % n_noise_) |
43 | 44 | print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels)) |
44 | 45 | print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels)) |
45 | 46 | print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels)) |
46 | | -print("Adjusted Rand Index: %0.3f" |
47 | | - % metrics.adjusted_rand_score(labels_true, labels)) |
48 | | -print("Adjusted Mutual Information: %0.3f" |
49 | | - % metrics.adjusted_mutual_info_score(labels_true, labels)) |
50 | | -print("Silhouette Coefficient: %0.3f" |
51 | | - % metrics.silhouette_score(X, labels)) |
| 47 | +print("Adjusted Rand Index: %0.3f" % metrics.adjusted_rand_score(labels_true, labels)) |
| 48 | +print( |
| 49 | + "Adjusted Mutual Information: %0.3f" |
| 50 | + % metrics.adjusted_mutual_info_score(labels_true, labels) |
| 51 | +) |
| 52 | +print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X, labels)) |
52 | 53 |
|
53 | 54 | # ############################################################################# |
54 | 55 | # Plot result |
55 | 56 | import matplotlib.pyplot as plt |
56 | 57 |
|
57 | 58 | # Black removed and is used for noise instead. |
58 | 59 | unique_labels = set(labels) |
59 | | -colors = [plt.cm.Spectral(each) |
60 | | - for each in np.linspace(0, 1, len(unique_labels))] |
| 60 | +colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))] |
61 | 61 | for k, col in zip(unique_labels, colors): |
62 | 62 | if k == -1: |
63 | 63 | # Black used for noise. |
64 | 64 | col = [0, 0, 0, 1] |
65 | 65 |
|
66 | | - class_member_mask = (labels == k) |
| 66 | + class_member_mask = labels == k |
67 | 67 |
|
68 | 68 | xy = X[class_member_mask & core_samples_mask] |
69 | | - plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), |
70 | | - markeredgecolor='k', markersize=14) |
| 69 | + plt.plot( |
| 70 | + xy[:, 0], |
| 71 | + xy[:, 1], |
| 72 | + "o", |
| 73 | + markerfacecolor=tuple(col), |
| 74 | + markeredgecolor="k", |
| 75 | + markersize=14, |
| 76 | + ) |
71 | 77 |
|
72 | 78 | xy = X[class_member_mask & ~core_samples_mask] |
73 | | - plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), |
74 | | - markeredgecolor='k', markersize=6) |
75 | | - |
76 | | -plt.title('Estimated number of clusters: %d' % n_clusters_) |
| 79 | + plt.plot( |
| 80 | + xy[:, 0], |
| 81 | + xy[:, 1], |
| 82 | + "o", |
| 83 | + markerfacecolor=tuple(col), |
| 84 | + markeredgecolor="k", |
| 85 | + markersize=6, |
| 86 | + ) |
| 87 | + |
| 88 | +plt.title("Estimated number of clusters: %d" % n_clusters_) |
77 | 89 | plt.show() |
0 commit comments