DBSCAN<T>

Models & Types · AiDotNet.Clustering.Density

DBSCAN (Density-Based Spatial Clustering of Applications with Noise) implementation.

Inheritance: ClusteringBase<T> → DBSCAN<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

DBSCAN finds clusters by looking for dense regions.

Imagine you’re at a party:

  • Dense groups of people talking are clusters
  • People standing alone are noise/outliers
  • DBSCAN finds these groups automatically

Key advantages:

  • Discovers clusters of any shape (not just circles)
  • Doesn’t require knowing the number of clusters
  • Identifies outliers as noise
  • Handles clusters of different sizes and densities

Limitations:

  • Sensitive to epsilon and MinPoints parameters
  • May struggle with clusters of varying density
  • O(n²) complexity without spatial indexing

How It Works

DBSCAN is a density-based clustering algorithm that groups together points that are closely packed together, marking points in low-density regions as outliers.

Algorithm steps:

  1. For each point, find all neighbors within epsilon distance
  2. If a point has at least MinPoints neighbors, it’s a core point
  3. Core points form cluster seeds
  4. Expand clusters by adding density-reachable points
  5. Mark remaining points as noise (-1)

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Density;
using AiDotNet.Tensors.LinearAlgebra;

var data = new Matrix<double>(6, 2);
double[][] rows = { new[] { 1.0, 1.0 }, new[] { 1.2, 0.9 }, new[] { 1.1, 1.1 },
                    new[] { 8.0, 8.0 }, new[] { 8.2, 7.9 }, new[] { 7.9, 8.1 } };
for (int i = 0; i < 6; i++) { data[i, 0] = rows[i][0]; data[i, 1] = rows[i][1]; }

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new DBSCAN<double>())
    .ConfigureDataLoader(DataLoaders.FromMatrix(data))
    .BuildAsync();

var labels = result.Predict(data);
Console.WriteLine($"DBSCAN: clustered {labels.Length} points.");

Constructors

Constructor Summary
DBSCAN(DBSCANOptions<T> options) Initializes a new DBSCAN instance with the specified options.

Properties

Property Summary
bool[] CorePointMask { get; } Gets the mask indicating which points are core points.
double Epsilon { get; } Gets the epsilon neighborhood radius.
int MinPoints { get; } Gets the minimum points for core point classification.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Matrix<T> Transform(Matrix<T> x)
ModelOptions GetOptions()
Vector<T> FitPredict(Matrix<T> x)
Vector<T> Predict(Matrix<T> x)
int GetNoiseCount() Gets the number of noise points (outliers).
int[] GetCoreSampleIndices() Gets the indices of core samples.
void Train(Matrix<T> x, Vector<T> y)

Fields

Field Summary
int NoiseLabel Noise label (points not assigned to any cluster).