DBSCAN Clustering : Machine Learning with Python
DBSCAN identifies clusters based on the density of data points. It groups together points that are close to each other and have a sufficient number of nearby neighbors. Points that are isolated or have low neighbor density are labeled as noise/outliers.
Advantages of DBSCAN over other clustering methods include:
- Robustness to parameter settings
- Ability to handle noise and outliers
- Flexibility in handling density variation
- Efficiency on Large Datasets
By visualizing the clusters obtained from DBSCAN, you can see how it effectively identifies and groups the moon-shaped clusters while labeling the data points outside the clusters as noise.
Get Hfahmida Data Science and Business Analytics:TRIDS’s stories in your inbox
Join Medium for free to get updates from this writer.
Below is an example of DBSCAN (Density-Based Spatial Clustering of Applications with Noise) clustering applied to a sample dataset.
# import the required libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_moons# Generate a moons-shaped dataset
n_samples = 200
X, _ = make_moons(n_samples=n_samples, noise=0.05, random_state=0)# Perform DBSCAN clustering
dbscan = DBSCAN(eps=0.3, min_samples=5)
labels = dbscan.fit_predict(X)# Plot the data points and their cluster assignments
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.title("DBSCAN Clustering")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
In this example, we generate a moons-shaped dataset using the make_moons function from scikit-learn. We then apply DBSCAN clustering to the dataset with eps=0.3 (the maximum distance between two samples to be considered in the same neighborhood) and min_samples=5 (the minimum number of samples required to form a dense region).
Output:
By visualizing the clusters obtained from DBSCAN, you can see how it effectively identifies and groups the moon-shaped clusters.







