DBSCAN Clustering in R

Introduction and Topic Overview

Welcome! In this lesson, we will explore the Density-Based Spatial Clustering of Applications with Noise (DBSCAN) algorithm. Using R and its powerful packages, we will implement DBSCAN and visualize its results with ggplot2. DBSCAN is a popular clustering algorithm that can identify clusters of varying shapes and sizes, as well as detect outliers (noise) in your data. In this lesson, we’ll use a synthetic "two moons" dataset, which is a classic example for demonstrating the strengths of density-based clustering. Let’s dive in and see how DBSCAN works in R!

Essential R Packages

To get started, we need to load a few essential R packages. The dbscan package provides the DBSCAN algorithm implementation, ggplot2 is used for data visualization, MASS helps with data generation, and scales is used for color palettes.

# Install packages if not already installed
if (!require(dbscan)) install.packages("dbscan")
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(MASS)) install.packages("MASS")
if (!require(scales)) install.packages("scales") # for hue_pal

# Load the libraries
library(dbscan)
library(ggplot2)
library(MASS)
library(scales)

Creating a Moon-Shaped Synthetic Dataset

To showcase DBSCAN’s ability to find clusters of arbitrary shapes, we’ll generate a "two moons" dataset. This dataset consists of two interleaving half circles, which are not well separated by traditional clustering algorithms like k-means.

set.seed(42)
n <- 150
theta <- runif(n, 0, pi)
x1 <- cbind(cos(theta), sin(theta)) + matrix(rnorm(2 * n, sd = 0.1), ncol = 2)
x2 <- cbind(1 - cos(theta), 1 - sin(theta) - 0.5) + matrix(rnorm(2 * n, sd = 0.1), ncol = 2)
moon_data <- rbind(x1, x2)
moon_data <- as.data.frame(moon_data)
colnames(moon_data) <- c("x", "y")

Here, we generate two moon-shaped clusters by sampling points along two half circles and adding a bit of noise for realism.

Standardizing Features

It’s a good practice to standardize features before clustering, especially when features are on different scales.

# Standardize features for clustering
moon_data_scaled <- as.data.frame(scale(moon_data))

Running DBSCAN

With our data ready, we can now apply the DBSCAN algorithm using the dbscan package. DBSCAN in R requires two main parameters: eps (the neighborhood radius) and minPts (the minimum number of points required to form a dense region).

# Parameters
epsilon <- 0.3
min_samples <- 5

# Run DBSCAN
db <- dbscan(moon_data_scaled, eps = epsilon, minPts = min_samples)

The cluster assignments are stored in db$cluster. In DBSCAN, points labeled as 0 are considered noise (outliers). To count the number of clusters (excluding noise):

# Number of clusters (excluding noise)
n_clusters <- length(setdiff(unique(db$cluster), 0))
cat("Estimated number of clusters:", n_clusters, "\n") # Should print 2

Visualizing DBSCAN Clusters with ggplot2

Example Plot Output

Here is an example of the plot you should see after running the visualization code above:

In this plot, each cluster is shown in a different color, and noise points (cluster 0) are displayed in black. The two moon-shaped clusters are clearly identified, demonstrating DBSCAN’s ability to find clusters of arbitrary shapes and to detect outliers.

Lesson Summary and Practice Exercises

Congratulations on successfully implementing the DBSCAN algorithm in R and visualizing the resulting clusters on a challenging "two moons" dataset! Practice is essential for mastering these concepts, so be sure to try out the upcoming exercises to reinforce your understanding. Good luck!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal