Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
1
import numpy as np
2
import matplotlib.pyplot as plt
3
from sklearn.cluster import DBSCAN
4
from sklearn.datasets import make_blobs
5
6
from .plot_helpers import discrete_scatter, cm3
7
8
9
def plot_dbscan():
10
X, y = make_blobs(random_state=0, n_samples=12)
11
12
dbscan = DBSCAN()
13
clusters = dbscan.fit_predict(X)
14
clusters
15
16
fig, axes = plt.subplots(3, 4, figsize=(11, 8),
17
subplot_kw={'xticks': (), 'yticks': ()})
18
# Plot clusters as red, green and blue, and outliers (-1) as white
19
colors = [cm3(1), cm3(0), cm3(2)]
20
markers = ['o', '^', 'v']
21
22
# iterate over settings of min_samples and eps
23
for i, min_samples in enumerate([2, 3, 5]):
24
for j, eps in enumerate([1, 1.5, 2, 3]):
25
# instantiate DBSCAN with a particular setting
26
dbscan = DBSCAN(min_samples=min_samples, eps=eps)
27
# get cluster assignments
28
clusters = dbscan.fit_predict(X)
29
print("min_samples: %d eps: %f cluster: %s"
30
% (min_samples, eps, clusters))
31
if np.any(clusters == -1):
32
c = ['w'] + colors
33
m = ['o'] + markers
34
else:
35
c = colors
36
m = markers
37
discrete_scatter(X[:, 0], X[:, 1], clusters, ax=axes[i, j], c=c,
38
s=8, markers=m)
39
inds = dbscan.core_sample_indices_
40
# vizualize core samples and clusters.
41
if len(inds):
42
discrete_scatter(X[inds, 0], X[inds, 1], clusters[inds],
43
ax=axes[i, j], s=15, c=colors,
44
markers=markers)
45
axes[i, j].set_title("min_samples: %d eps: %.1f"
46
% (min_samples, eps))
47
fig.tight_layout()
48
49