Module 4 of 25 · Unsupervised Learning — K-Means, DBSCAN, Hierarchical Clustering, PCA, t-SNE, Autoencoders · Beginner

DBSCAN Clustering Fundamentals

Duration: 5 min

This module delves into the fundamentals of DBSCAN (Density-Based Spatial Clustering of Applications with Noise), a powerful unsupervised machine learning algorithm used for clustering. Understanding DBSCAN is crucial for identifying clusters in datasets where the number of clusters is not predefined and the clusters can be of arbitrary shape. This module will cover the core concepts, parameters, and practical implementation of DBSCAN using Python.

Understanding DBSCAN

DBSCAN is a density-based clustering algorithm that groups together points that are packed closely together, marking as outliers points that lie alone in low-density regions. The algorithm requires two main parameters: eps (epsilon), which defines the maximum distance between two samples for them to be considered as in the same neighborhood, and min_samples, the number of samples in a neighborhood for a point to be considered as a core point. DBSCAN is particularly effective in datasets with clusters of varying densities and shapes.

from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

# Generate sample data
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# Apply DBSCAN
dbsc = DBSCAN(eps=0.3, min_samples=5)
dbsc.fit(X)

# Plotting the results
plt.scatter(X[:, 0], X[:, 1], c=dbsc.labels_, cmap='viridis')
plt.title('DBSCAN Clustering')
plt.show()

Try it in Google Colab: Open in Colab

A scatter plot with different clusters represented by different colors and noise points marked in black.

Choosing Parameters for DBSCAN

Selecting appropriate values for eps and min_samples is critical for the performance of DBSCAN. The value of eps determines the maximum distance between two samples for them to be considered as in the same neighborhood. A smaller eps value leads to more clusters, while a larger value may merge different clusters. The min_samples parameter defines the minimum number of points required to form a dense region; increasing this value tends to lead towards larger clusters, reducing the number of noise points.

from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

# Generate sample data
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# Apply DBSCAN with different parameters
dbsc = DBSCAN(eps=0.5, min_samples=10)
dbsc.fit(X)

# Plotting the results
plt.scatter(X[:, 0], X[:, 1], c=dbsc.labels_, cmap='viridis')
plt.title('DBSCAN Clustering with Different Parameters')
plt.show()

💡 Tip: Experiment with different values of eps and min_samples to find the best parameters for your specific dataset. Visual inspection of the resulting clusters can help in tuning these parameters effectively.

❓ What does the `eps` parameter in DBSCAN control?

❓ How does increasing the `min_samples` parameter affect DBSCAN clustering?

← Previous Continue interactively → Next →

Related Courses