Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn OPTICS

From Leeroopedia


Knowledge Sources
Domains Clustering, Density-Based Clustering
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for performing ordering points to identify clustering structure (OPTICS) provided by scikit-learn.

Description

OPTICS is a density-based clustering algorithm closely related to DBSCAN that finds core samples of high density and expands clusters from them. Unlike DBSCAN, OPTICS keeps the cluster hierarchy for a variable neighborhood radius, making it better suited for datasets with clusters of varying densities. Clusters are extracted from the computed cluster-order using either a DBSCAN-like method or the automated xi technique. The implementation first performs k-nearest-neighbor searches on all points, then computes reachability distances to construct the cluster order.

Usage

Use OPTICS when you expect clusters of varying density, when you want to explore the hierarchical density structure of data, or when DBSCAN fails because a single eps value cannot capture clusters at different density scales. It is well-suited for large datasets and spatial data analysis.

Code Reference

Source Location

Signature

class OPTICS(ClusterMixin, BaseEstimator):
    def __init__(
        self,
        *,
        min_samples=5,
        max_eps=np.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,
    ):

Import

from sklearn.cluster import OPTICS

I/O Contract

Inputs

Name Type Required Description
min_samples int or float No Number of samples in a neighborhood for a core point. Default is 5.
max_eps float No Maximum distance between two samples for neighborhood. Default is np.inf.
metric str or callable No Distance metric. Default is "minkowski".
p int No Power parameter for the Minkowski metric. Default is 2.
metric_params dict or None No Additional keyword arguments for the metric function. Default is None.
cluster_method str No Extraction method: "xi" or "dbscan". Default is "xi".
eps float or None No Maximum distance for DBSCAN cluster extraction method. Default is None.
xi float No Steepness threshold for xi cluster extraction (between 0 and 1). Default is 0.05.
predecessor_correction bool No Correct clusters based on predecessor info. Default is True.
min_cluster_size int or float or None No Minimum number of samples in a cluster. Default is None (uses min_samples).
algorithm str No Nearest neighbor algorithm: "auto", "ball_tree", "kd_tree", or "brute". Default is "auto".
leaf_size int No Leaf size for BallTree or KDTree. Default is 30.
memory str or joblib.Memory No Used to cache nearest neighbor computations. Default is None.
n_jobs int or None No Number of parallel jobs. Default is None.

Outputs

Name Type Description
labels_ ndarray of shape (n_samples,) Cluster labels. Noisy samples and unclustered points are labeled -1.
reachability_ ndarray of shape (n_samples,) Reachability distances per sample, indexed by ordering_.
ordering_ ndarray of shape (n_samples,) The cluster-ordered list of sample indices.
core_distances_ ndarray of shape (n_samples,) Distance to the min_samples-th nearest neighbor for each sample.
predecessor_ ndarray of shape (n_samples,) Point that a sample was reached from, indexed by ordering_.
cluster_hierarchy_ ndarray of shape (n_clusters, 2) List of clusters with start and end indices in the reachability plot (xi method only).

Usage Examples

Basic Usage

from sklearn.cluster import OPTICS
import numpy as np

X = np.array([[1, 2], [2, 5], [3, 6],
              [8, 7], [8, 8], [7, 3]])

clustering = OPTICS(min_samples=2).fit(X)
print(clustering.labels_)
print(clustering.reachability_)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment