Density-based clustering with uneven structure
Clusters have different densities or non-flat geometry.
Scikit-Learn · Class
OPTICS estimates clustering structure while preserving density information over a range of neighborhood radii. It extracts clusters with either the Xi method or a DBSCAN-like method.
OPTICS orders samples to represent density structure over a range of neighborhood radii, then extracts labels using either the Xi method or a DBSCAN-style threshold.
class sklearn.cluster.OPTICS(*, min_samples=5, max_eps=inf, metric='minkowski', p=2, metric_params=None, cluster_method='xi', eps=None, xi=0.05, predecessor_correction=True, min_cluster_size=None, algorithm='auto', leaf_size=30, memory=None, n_jobs=None)| Name | Type | Description |
|---|---|---|
min_samples | int or float | Minimum neighborhood sample count; an integer greater than 1 or a fraction from 0 to 1. Default: 5. |
max_eps | float | Maximum radius used when searching for reachable neighbors. Default: infinity. |
metric | str or callable | Distance metric used for neighborhood calculations. Default: 'minkowski'. |
p | int | Minkowski metric power parameter. Default: 2. |
metric_params | dict or None | Additional keyword parameters for the metric. Default: None. |
cluster_method | str | Cluster extraction strategy: 'xi' or 'dbscan'. Default: 'xi'. |
eps | float or None | DBSCAN-style extraction radius. Default: None. |
xi | float | Minimum steepness used by Xi extraction. Default: 0.05. |
predecessor_correction | bool | Whether to apply predecessor correction during Xi extraction. Default: True. |
min_cluster_size | int or float or None | Minimum cluster size, expressed as a count or fraction. Default: None. |
algorithm | str | Nearest-neighbor search algorithm. Default: 'auto'. |
leaf_size | int | Leaf size for tree-based neighbor searches. Default: 30. |
After fitting, the estimator provides labels_ and ordering-related reachability information; fit_predict returns an array of estimated labels with shape (n_samples,).
from sklearn.cluster import OPTICS
import numpy as np
X = np.array([
[0.0, 0.0], [0.1, 0.0], [0.0, 0.1],
[5.0, 5.0], [5.1, 5.0], [5.0, 5.1],
])
model = OPTICS(min_samples=2, cluster_method="xi", xi=0.05)
labels = model.fit_predict(X)
print(labels)Fits OPTICS and prints the cluster label assigned to each sample.
Clusters have different densities or non-flat geometry.
A dataset needs clustering over many possible neighborhood scales.
Noise or anomalous samples should remain separate from dense groups.
A fitted reachability representation will be reused for DBSCAN-style thresholds.
OPTICS finds core samples in dense regions, expands clusters, and preserves a hierarchy across variable neighborhood radii. It then extracts clusters with either the Xi method or DBSCAN-style extraction.
Follow-up: How can DBSCAN-style labels be extracted for another threshold? Use the fitted reachability graph with cluster_optics_dbscan; the chosen eps must be below max_eps.
The ordering_ attribute stores the order in which samples appear in the reachability representation, while reachability_ stores each sample’s reachability distance. Together they support cluster-membership extraction after fitting.
Follow-up: Why is max_eps relevant? It bounds the neighborhood radius used to find potentially reachable points and a smaller bound can reduce runtime.
cluster_method='xi' uses the automatic Xi-steep extraction technique, whereas cluster_method='dbscan' extracts clusters with a DBSCAN-like method using eps. The default cluster method is Xi.
Follow-up: What constraint applies to eps in DBSCAN-style extraction? It must be strictly less than max_eps.
DBSCANcluster_optics_dbscancluster_optics_xicompute_optics_graph