mojolearn.OPTICS#

class mojolearn.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)[source]#

Bases: _XCluster

OPTICS density-based clustering.

Follows scikit-learn’s OPTICS: it orders the rows by reachability and extracts clusters with the ‘xi’ or ‘dbscan’ method. The full n_samples x n_samples distance matrix is computed, so memory grows with the square of the number of rows. Computation is float32 throughout, without scikit-learn’s rounding of distances to float precision, so reachability agrees with scikit-learn’s within a tolerance. Like scikit-learn’s, it has no predict.

Parameters:
  • min_samples (int > 1 or float in (0, 1], default 5) – Neighborhood size for a core point, as a count or as a fraction of the number of samples (at least 2).

  • max_eps (float, default inf) – Largest neighborhood radius considered.

  • metric (str, default ‘minkowski’) – One of ‘minkowski’, ‘euclidean’, ‘l2’, ‘manhattan’, ‘cityblock’, ‘l1’, ‘chebyshev’, ‘infinity’, ‘cosine’ or ‘precomputed’. Any other metric, including a callable, raises NotImplementedError. With ‘precomputed’, X is a square distance matrix.

  • p (float, default 2) – Power of the Minkowski metric; must be at least 1 (inf gives Chebyshev).

  • metric_params (dict or None, default None) – Only the key ‘p’ is read; it overrides p.

  • cluster_method ({‘xi’, ‘dbscan’}, default ‘xi’) – How clusters are extracted from the reachability ordering.

  • eps (float or None, default None) – Radius for cluster_method='dbscan'; None means max_eps. Must not exceed max_eps.

  • xi (float in [0, 1], default 0.05) – Minimum steepness for cluster_method='xi'.

  • predecessor_correction (bool, default True) – Apply scikit-learn’s predecessor correction in the xi extraction.

  • min_cluster_size (int > 1, float in (0, 1] or None, default None) – Minimum cluster size for the xi method; None means min_samples.

  • algorithm (str, default ‘auto’) – Accepted for scikit-learn compatibility; ignored.

  • leaf_size (int, default 30) – Accepted for scikit-learn compatibility; ignored.

  • memory (None) – Accepted for scikit-learn compatibility; ignored.

  • n_jobs (None) – Accepted for scikit-learn compatibility; ignored.

  • numeric_mode (str or None, default None) – Numeric mode for this estimator. None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • labels_ (Array of shape (n_samples,), int32) – Cluster of each row; -1 marks noise.

  • reachability_ (Array of shape (n_samples,), float32) – Reachability distance of each row, indexed by row.

  • ordering_ (Array of shape (n_samples,), int32) – Cluster ordering of the rows.

  • core_distances_ (Array of shape (n_samples,), float32) – Distance at which each row becomes a core point.

  • predecessor_ (Array of shape (n_samples,), int32) – Row from which each row was reached; -1 for none.

  • cluster_hierarchy_ (Array of shape (n_clusters, 2), int32) – Start and end positions (in ordering_) of each cluster. Set only with cluster_method='xi'.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((500, 4), dtype=np.float32)
>>> labels = mojolearn.OPTICS(min_samples=10).fit_predict(X)
__init__(*, 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)[source]#

Methods

fit(X[, y])

Compute the reachability ordering of X and extract clusters.

fit_predict(X[, y])

Fit the model on X and return the cluster labels.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None)[source]#

Compute the reachability ordering of X and extract clusters.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training rows, or a square distance matrix with metric='precomputed'. Converted to float32; NaN or infinity raises ValueError.

  • y (ignored) – Present for scikit-learn compatibility.

Returns:

self (OPTICS) – The fitted estimator.

Raises:
  • NotImplementedError – If metric is not supported.

  • ValueError – If cluster_method, min_samples, min_cluster_size, eps, xi or p is out of range, or a precomputed matrix is not square.

fit_predict(X, y=None)[source]#

Fit the model on X and return the cluster labels.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training rows, or a square distance matrix with metric='precomputed'.

  • y (ignored) – Present for scikit-learn compatibility.

Returns:

labels (Array of shape (n_samples,), int32) – The fitted labels_; -1 marks noise.