Skip to content

Instantly share code, notes, and snippets.

@gabriellm1
Last active November 23, 2020 20:15
Show Gist options
  • Save gabriellm1/82e0bdd7e3c843463732dea7a0f66188 to your computer and use it in GitHub Desktop.
Save gabriellm1/82e0bdd7e3c843463732dea7a0f66188 to your computer and use it in GitHub Desktop.
DBSCAN_test
from sklearn import cluster, datasets
n_samples = 1000
noisy_moons, _ = datasets.make_moons(n_samples=n_samples, noise=.05)
noisy_circles, _ = datasets.make_circles(n_samples=n_samples, factor=.5, noise=.05)
blobs1, _ = datasets.make_blobs(n_samples=n_samples, random_state=10, center_box=(-2, 2),cluster_std=0.2)
blobs2, _ = datasets.make_blobs(n_samples=n_samples, random_state=4, center_box=(-2, 2),cluster_std=0.4)
blobs3, _ = datasets.make_blobs(n_samples=n_samples, random_state=7, center_box=(-2, 2),cluster_std=0.3)
no_structure, _ = np.random.rand(n_samples, 2), None
examples = [noisy_moons, noisy_circles, blobs1, blobs2, blobs3, no_structure]
from sklearn.cluster import DBSCAN
plt.figure(figsize=(10, 10))
colours = {}
colours[0] = 'r'
colours[1] = 'g'
colours[2] = 'b'
colours[3] = 'y'
colours[4] = 'c'
colours[5] = 'm'
colours[6] = 'tab:pink'
colours[7] = 'tab:orange'
colours[8] = 'tab:olive'
colours[9] = 'tab:brown'
colours[-1] = 'k'
for num, i in enumerate(examples):
db_default = DBSCAN(eps = 0.2, min_samples = 20).fit(i)
labels = db_default.labels_
cvec = [colours[label] for label in labels]
plt.subplot(3, 2, num+1)
plt.scatter(i[:, 0], i[:, 1], color = cvec)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment