Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 63

DBSCAN Clustering: Implementation and Visualization

Recommended resources

Welcome back! In our previous lesson, we dove into the theory of DBSCAN, understanding how it leverages density to define clusters and identify noise. You learned about the crucial roles of the eps and minPts parameters and how they are used to classify points as core, border, or noise.

Today, we'll transition from theory to practice. This lesson is all about applying what you've learned. Your objective is to use Python's scikit-learn library to implement DBSCAN on a real-world dataset. We will walk through the entire process, from preparing the data and choosing parameters to running the algorithm and, most importantly, visualizing the clusters and the noise points it identifies.

Implementing DBSCAN with scikit-learn

Given your experience with Python and its data science libraries, you'll find the scikit-learn implementation of DBSCAN to be quite straightforward. The primary class is DBSCAN, located in the sklearn.cluster module.

The main parameters you'll need to provide when creating a DBSCAN object are the ones we discussed:

  • eps: The maximum distance between two samples for one to be considered as in the neighborhood of the other.
  • min_samples: The number of samples in a neighborhood for a point to be considered as a core point. This is the minPts we discussed.

The video "DBSCAN Algorithm | Machine Learning with Scikit-Learn Python" provides a quick and clear demonstration of the basic syntax.

DBSCAN Algorithm | Machine Learning with Scikit-Learn Python

This video from the Normalized Nerd channel shows the essential scikit-learn implementation steps.

First, watch the segment on implementing DBSCAN. Notice how the DBSCAN object is instantiated and then fitted to the data using .fit_predict(). Next, watch the segment on interpreting the output. Pay close attention to the special cluster label -1, which scikit-learn uses to denote noise points. This is a key feature of the algorithm's output.


Practical Application: Customer Segmentation

Now, let's apply DBSCAN to a practical problem: segmenting customers based on their shopping behavior. We will use the 'Mall Customers' dataset, which contains features like Annual Income and Spending Score. This is a classic clustering problem where identifying natural groupings and outliers can provide valuable business insights.

The Medium article "DBSCAN: The Hidden Gem of Clustering" provides an excellent end-to-end walkthrough that we will follow.

DBSCAN: The Hidden Gem of Clustering — From Basics to ...

This section of the article contains the full Python code for loading the data, scaling it, running DBSCAN, and visualizing the clusters.

Navigate to the section implementation. I encourage you to set up a Jupyter Notebook or a Python script and follow along with the code provided. As you work through it, focus on these key steps: Data Loading and Selection: The code loads the dataset directly from a URL and selects the 'Annual Income' and 'Spending Score' columns. Feature Scaling: Notice the use of StandardScaler. Since DBSCAN is a distance-based algorithm, it is crucial that all features are on a similar scale. Otherwise, features with larger ranges will disproportionately influence the distance calculations. Applying DBSCAN: The DBSCAN model is instantiated with eps=0.5 and min_samples=5. The .fit_predict() method is then called on the scaled data to get the cluster labels. Visualization: The code uses matplotlib to create a scatter plot. The c=df['Cluster'] argument is what colors each point according to its assigned cluster. Notice how the noise points (label -1) will automatically be colored differently, making them easy to spot.

After running the code, you should see a plot that groups customers into several clusters, with some points marked as noise. These noise points represent customers whose spending habits don't fit neatly into any of the main groups.

An example of DBSCAN applied to a dataset. The original, unclustered data is on the left. The plot on the right shows the output, with different colors representing the clusters found by the algorithm. The small black dots are the points classified as noise.

A Note on Choosing Parameters

In the example above, the parameters (eps=0.5, min_samples=5) were chosen for you. However, as we discussed in the last lesson, selecting the right eps is the most critical and challenging part of using DBSCAN. The k-distance graph is the standard heuristic for this.

Let's look at how to generate this plot in Python. The same Medium article provides a clear code snippet for this.

DBSCAN: The Hidden Gem of Clustering — From Basics to ...

This section demonstrates the k-distance plot method for finding an optimal eps.

Now, review the section Choosing eps. The code uses sklearn.neighbors.NearestNeighbors to calculate the distance of each point to its k-th nearest neighbor (where k is set by min_samples). These distances are then sorted and plotted. The code also uses the kneed library to programmatically find the "elbow" or "knee" of the curve. This point of maximum curvature represents a threshold where distances start increasing sharply, suggesting a good value for eps.

By applying this technique, you can make a much more informed decision about your eps value instead of just guessing.


Connecting Visualization to Theory

The clusters and noise points you see on your plot are a direct result of the core concepts we discussed. Let's revisit the classification of points to solidify this connection.

This diagram illustrates the three types of points in DBSCAN. The blue squares are core points because they have at least `MinPts=3` neighbors within their `eps` radius. The filled black circles are border points because they are in the neighborhood of a core point but don't have enough neighbors themselves. The empty white circles are noise points, as they are isolated.

When you look at your visualization of the mall customer data:

  • The points inside the colorful clusters are a mix of core points and border points. The algorithm started with core points and expanded the clusters to include reachable border points.
  • The points colored differently (typically black, corresponding to the -1 label) are the noise points. The algorithm found them to be too isolated to belong to any dense region.

Conclusion

In this lesson, you successfully bridged the gap between the theory of DBSCAN and its practical application. You've learned how to implement the algorithm using scikit-learn, the importance of scaling your data, and how to visualize the results to identify both the clusters and the outliers.

Key Takeaways:

  • DBSCAN is available in scikit-learn as sklearn.cluster.DBSCAN.
  • Feature scaling (e.g., using StandardScaler) is a critical preprocessing step for this distance-based algorithm.
  • The .fit_predict() method returns an array of cluster labels, where -1 is the special label for noise points.
  • Visualizing the output with a scatter plot, coloring points by their cluster label, is the most effective way to interpret the results of DBSCAN.
  • Heuristics like the k-distance plot can be implemented in Python to help guide the selection of the eps parameter.

What's Next:

Over the past few lessons, you have learned three distinct clustering algorithms: K-Means, Hierarchical Agglomerative Clustering, and DBSCAN. Each has its own mechanism, strengths, and weaknesses. In our next and final lesson of this module, we will bring it all together by applying all three algorithms to the same dataset. This will allow you to directly compare their results and build a strong intuition for when to choose one over the others.