2011-07-12 20:45:20 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
===================================
|
|
|
|
|
Demo of DBSCAN clustering algorithm
|
|
|
|
|
===================================
|
|
|
|
|
|
2011-08-06 16:11:57 +08:00
|
|
|
Finds core samples of high density and expands clusters from them.
|
2011-07-12 20:45:20 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2013-04-05 03:47:21 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.cluster import DBSCAN
|
|
|
|
|
from sklearn import metrics
|
2019-10-28 05:17:23 +08:00
|
|
|
from sklearn.datasets import make_blobs
|
2013-04-05 03:47:21 +08:00
|
|
|
from sklearn.preprocessing import StandardScaler
|
2011-07-12 20:45:20 +08:00
|
|
|
|
|
|
|
|
|
2022-03-03 17:41:52 +08:00
|
|
|
# %%
|
2011-07-12 20:45:20 +08:00
|
|
|
# Generate sample data
|
2022-03-03 17:41:52 +08:00
|
|
|
# --------------------
|
2011-07-12 20:45:20 +08:00
|
|
|
centers = [[1, 1], [-1, -1], [1, -1]]
|
2013-04-05 03:47:21 +08:00
|
|
|
X, labels_true = make_blobs(
|
|
|
|
|
n_samples=750, centers=centers, cluster_std=0.4, random_state=0
|
|
|
|
|
)
|
2011-07-12 20:45:20 +08:00
|
|
|
|
2013-04-05 03:47:21 +08:00
|
|
|
X = StandardScaler().fit_transform(X)
|
2011-07-12 20:45:20 +08:00
|
|
|
|
2022-03-03 17:41:52 +08:00
|
|
|
# %%
|
2011-07-12 20:45:20 +08:00
|
|
|
# Compute DBSCAN
|
2022-03-03 17:41:52 +08:00
|
|
|
# --------------
|
2013-04-05 03:47:21 +08:00
|
|
|
db = DBSCAN(eps=0.3, min_samples=10).fit(X)
|
2013-10-10 15:37:31 +08:00
|
|
|
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
|
|
|
|
|
core_samples_mask[db.core_sample_indices_] = True
|
2011-07-12 20:45:20 +08:00
|
|
|
labels = db.labels_
|
|
|
|
|
|
|
|
|
|
# Number of clusters in labels, ignoring noise if present.
|
|
|
|
|
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
|
2018-10-23 04:56:53 +08:00
|
|
|
n_noise_ = list(labels).count(-1)
|
2011-07-12 20:45:20 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Estimated number of clusters: %d" % n_clusters_)
|
2018-10-23 04:56:53 +08:00
|
|
|
print("Estimated number of noise points: %d" % n_noise_)
|
2013-02-01 22:04:03 +08:00
|
|
|
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"
|
2019-05-09 08:35:32 +08:00
|
|
|
% metrics.adjusted_mutual_info_score(labels_true, labels)
|
2021-10-07 16:13:00 +08:00
|
|
|
)
|
2013-04-05 03:47:21 +08:00
|
|
|
print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X, labels))
|
2011-07-12 20:45:20 +08:00
|
|
|
|
2022-03-03 17:41:52 +08:00
|
|
|
# %%
|
2011-07-12 20:45:20 +08:00
|
|
|
# Plot result
|
2022-03-03 17:41:52 +08:00
|
|
|
# -----------
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-07-12 20:45:20 +08:00
|
|
|
|
|
|
|
|
# Black removed and is used for noise instead.
|
2013-04-05 03:47:21 +08:00
|
|
|
unique_labels = set(labels)
|
2017-06-09 06:16:54 +08:00
|
|
|
colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))]
|
2013-04-05 03:47:21 +08:00
|
|
|
for k, col in zip(unique_labels, colors):
|
2011-07-12 20:45:20 +08:00
|
|
|
if k == -1:
|
|
|
|
|
# Black used for noise.
|
2017-06-18 22:33:10 +08:00
|
|
|
col = [0, 0, 0, 1]
|
2013-10-10 15:37:31 +08:00
|
|
|
|
|
|
|
|
class_member_mask = labels == k
|
|
|
|
|
|
|
|
|
|
xy = X[class_member_mask & core_samples_mask]
|
2017-06-18 22:33:10 +08:00
|
|
|
plt.plot(
|
|
|
|
|
xy[:, 0],
|
|
|
|
|
xy[:, 1],
|
|
|
|
|
"o",
|
|
|
|
|
markerfacecolor=tuple(col),
|
2014-05-15 10:35:13 +08:00
|
|
|
markeredgecolor="k",
|
|
|
|
|
markersize=14,
|
|
|
|
|
)
|
2013-10-10 15:37:31 +08:00
|
|
|
|
|
|
|
|
xy = X[class_member_mask & ~core_samples_mask]
|
2017-06-18 22:33:10 +08:00
|
|
|
plt.plot(
|
|
|
|
|
xy[:, 0],
|
|
|
|
|
xy[:, 1],
|
|
|
|
|
"o",
|
|
|
|
|
markerfacecolor=tuple(col),
|
2014-05-15 10:35:13 +08:00
|
|
|
markeredgecolor="k",
|
|
|
|
|
markersize=6,
|
|
|
|
|
)
|
2021-10-07 16:13:00 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title("Estimated number of clusters: %d" % n_clusters_)
|
|
|
|
|
plt.show()
|