Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

OPTICS

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
min_samplesint or floatMinimum neighborhood sample count; an integer greater than 1 or a fraction from 0 to 1. Default: 5.
max_epsfloatMaximum radius used when searching for reachable neighbors. Default: infinity.
metricstr or callableDistance metric used for neighborhood calculations. Default: 'minkowski'.
pintMinkowski metric power parameter. Default: 2.
metric_paramsdict or NoneAdditional keyword parameters for the metric. Default: None.
cluster_methodstrCluster extraction strategy: 'xi' or 'dbscan'. Default: 'xi'.
epsfloat or NoneDBSCAN-style extraction radius. Default: None.
xifloatMinimum steepness used by Xi extraction. Default: 0.05.
predecessor_correctionboolWhether to apply predecessor correction during Xi extraction. Default: True.
min_cluster_sizeint or float or NoneMinimum cluster size, expressed as a count or fraction. Default: None.
algorithmstrNearest-neighbor search algorithm. Default: 'auto'.
leaf_sizeintLeaf size for tree-based neighbor searches. Default: 30.

Returns

After fitting, the estimator provides labels_ and ordering-related reachability information; fit_predict returns an array of estimated labels with shape (n_samples,).

Key points

  • OPTICS is an estimator for discovering clustering structure in a feature matrix.
  • Its clustering process identifies dense core samples and grows clusters from those cores.
  • Unlike a single-radius approach, it retains hierarchy across changing neighborhood radii.
  • After ordering samples, OPTICS can derive clusters with a DBSCAN-like extraction.
  • The alternative extraction is the automatic Xi technique, selected with cluster_method='xi'.
  • min_samples controls how many neighboring samples are needed for a point to qualify as core.

Common mistakes

  • The implementation is not identical to the original algorithm because it computes core sizes for all points before constructing the ordering.
  • Reducing max_eps can improve runtime, but it limits the neighborhood radius considered during graph construction.

Example: Cluster points with OPTICS

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.

When to use it

Density-based clustering with uneven structure

Clusters have different densities or non-flat geometry.

Variable-radius density analysis

A dataset needs clustering over many possible neighborhood scales.

Outlier-aware clustering

Noise or anomalous samples should remain separate from dense groups.

Transductive threshold exploration

A fitted reachability representation will be reused for DBSCAN-style thresholds.

Interview questions

QWhat problem does OPTICS solve, and how does it differ from DBSCAN?

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.

QWhat are ordering_ and reachability_ used for?

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.

QHow do the two cluster extraction methods differ?

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.

  • DBSCAN
  • cluster_optics_dbscan
  • cluster_optics_xi
  • compute_optics_graph

Source: OPTICS — scikit-learn 1.9.1 documentation. Checked 2026-10-15.