OPTICS#

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

Estimate clustering structure from vector array.

OPTICS (Ordering Points To Identify the Clustering Structure), closelyrelated to DBSCAN, finds core samples of high density and expands clustersfrom them[1]. Unlike DBSCAN, it keeps cluster hierarchy for a variableneighborhood radius. Better suited for usage on large datasets than thecurrent scikit-learn implementation of DBSCAN.

Clusters are then extracted from the cluster-order using aDBSCAN-like method (cluster_method = ‘dbscan’) or an automatictechnique proposed in[1] (cluster_method = ‘xi’).

This implementation deviates from the original OPTICS by first performingk-nearest-neighborhood searches on all points to identify core sizes ofall points (instead of computing neighbors while looping through points).Reachability distances to only unprocessed points are then computed, toconstruct the cluster order, similar to the original OPTICS.Note that we do not employ a heap to manage the expansioncandidates, so the time complexity will be O(n^2).

Read more in theUser Guide.

Parameters:
min_samplesint > 1 or float between 0 and 1, default=5

The number of samples in a neighborhood for a point to be considered asa core point. Also, up and down steep regions can’t have more thanmin_samples consecutive non-steep points. Expressed as an absolutenumber or a fraction of the number of samples (rounded to be at least2).

max_epsfloat, default=np.inf

The maximum distance between two samples for one to be considered asin the neighborhood of the other. Default value ofnp.inf willidentify clusters across all scales; reducingmax_eps will resultin shorter run times.

metricstr or callable, default=’minkowski’

Metric to use for distance computation. Any metric from scikit-learnorscipy.spatial.distance can be used.

Ifmetric is a callable function, it is called on eachpair of instances (rows) and the resulting value recorded. The callableshould take two arrays as input and return one value indicating thedistance between them. This works for Scipy’s metrics, but is lessefficient than passing the metric name as a string. If metric is“precomputed”,X is assumed to be a distance matrix and must besquare.

Valid values for metric are:

  • from scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’,‘manhattan’]

  • from scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’,‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’,‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’,‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’,‘yule’]

Sparse matrices are only supported by scikit-learn metrics.Seescipy.spatial.distance for details on these metrics.

Note

'kulsinski' is deprecated from SciPy 1.9 and will be removed in SciPy 1.11.

pfloat, default=2

Parameter for the Minkowski metric frompairwise_distances. When p = 1, this isequivalent to using manhattan_distance (l1), and euclidean_distance(l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

metric_paramsdict, default=None

Additional keyword arguments for the metric function.

cluster_method{‘xi’, ‘dbscan’}, default=’xi’

The extraction method used to extract clusters using the calculatedreachability and ordering.

epsfloat, default=None

The maximum distance between two samples for one to be considered asin the neighborhood of the other. By default it assumes the same valueasmax_eps.Used only whencluster_method='dbscan'.

xifloat between 0 and 1, default=0.05

Determines the minimum steepness on the reachability plot thatconstitutes a cluster boundary. For example, an upwards point in thereachability plot is defined by the ratio from one point to itssuccessor being at most 1-xi.Used only whencluster_method='xi'.

predecessor_correctionbool, default=True

Correct clusters according to the predecessors calculated by OPTICS[2]. This parameter has minimal effect on most datasets.Used only whencluster_method='xi'.

min_cluster_sizeint > 1 or float between 0 and 1, default=None

Minimum number of samples in an OPTICS cluster, expressed as anabsolute number or a fraction of the number of samples (rounded to beat least 2). IfNone, the value ofmin_samples is used instead.Used only whencluster_method='xi'.

algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’

Algorithm used to compute the nearest neighbors:

  • ‘ball_tree’ will useBallTree.

  • ‘kd_tree’ will useKDTree.

  • ‘brute’ will use a brute-force search.

  • ‘auto’ (default) will attempt to decide the most appropriatealgorithm based on the values passed tofit method.

Note: fitting on sparse input will override the setting ofthis parameter, using brute force.

leaf_sizeint, default=30

Leaf size passed toBallTree orKDTree. This can affect the speed of theconstruction and query, as well as the memory required to store thetree. The optimal value depends on the nature of the problem.

memorystr or object with the joblib.Memory interface, default=None

Used to cache the output of the computation of the tree.By default, no caching is done. If a string is given, it is thepath to the caching directory.

n_jobsint, default=None

The number of parallel jobs to run for neighbors search.None means 1 unless in ajoblib.parallel_backend context.-1 means using all processors. SeeGlossaryfor more details.

Attributes:
labels_ndarray of shape (n_samples,)

Cluster labels for each point in the dataset given to fit().Noisy samples and points which are not included in a leaf clusterofcluster_hierarchy_ are labeled as -1.

reachability_ndarray of shape (n_samples,)

Reachability distances per sample, indexed by object order. Useclust.reachability_[clust.ordering_] to access in cluster order.

ordering_ndarray of shape (n_samples,)

The cluster ordered list of sample indices.

core_distances_ndarray of shape (n_samples,)

Distance at which each sample becomes a core point, indexed by objectorder. Points which will never be core have a distance of inf. Useclust.core_distances_[clust.ordering_] to access in cluster order.

predecessor_ndarray of shape (n_samples,)

Point that a sample was reached from, indexed by object order.Seed points have a predecessor of -1.

cluster_hierarchy_ndarray of shape (n_clusters, 2)

The list of clusters in the form of[start,end] in each row, withall indices inclusive. The clusters are ordered according to(end,-start) (ascending) so that larger clusters encompassingsmaller clusters come after those smaller ones. Sincelabels_ doesnot reflect the hierarchy, usuallylen(cluster_hierarchy_)>np.unique(optics.labels_). Please alsonote that these indices are of theordering_, i.e.X[ordering_][start:end+1] form a cluster.Only available whencluster_method='xi'.

n_features_in_int

Number of features seen duringfit.

Added in version 0.24.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen duringfit. Defined only whenXhas feature names that are all strings.

Added in version 1.0.

See also

DBSCAN

A similar clustering for a specified neighborhood radius (eps). Our implementation is optimized for runtime.

References

[1](1,2)

Ankerst, Mihael, Markus M. Breunig, Hans-Peter Kriegel,and Jörg Sander. “OPTICS: ordering points to identify the clusteringstructure.” ACM SIGMOD Record 28, no. 2 (1999): 49-60.

[2]

Schubert, Erich, Michael Gertz.“Improving the Cluster Structure Extracted from OPTICS Plots.” Proc. ofthe Conference “Lernen, Wissen, Daten, Analysen” (LWDA) (2018): 318-329.

Examples

>>>fromsklearn.clusterimportOPTICS>>>importnumpyasnp>>>X=np.array([[1,2],[2,5],[3,6],...[8,7],[8,8],[7,3]])>>>clustering=OPTICS(min_samples=2).fit(X)>>>clustering.labels_array([0, 0, 0, 1, 1, 1])

For a more detailed example seeDemo of OPTICS clustering algorithm.

For a comparison of OPTICS with other clustering algorithms, seeComparing different clustering algorithms on toy datasets

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

Perform OPTICS clustering.

Extracts an ordered list of points and reachability distances, andperforms initial clustering usingmax_eps distance specified atOPTICS object instantiation.

Parameters:
X{ndarray, sparse matrix} of shape (n_samples, n_features), or (n_samples, n_samples) if metric=’precomputed’

A feature array, or array of distances between samples ifmetric=’precomputed’. If a sparse matrix is provided, it will beconverted into CSR format.

yIgnored

Not used, present for API consistency by convention.

Returns:
selfobject

Returns a fitted instance of self.

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

Perform clustering onX and returns cluster labels.

Parameters:
Xarray-like of shape (n_samples, n_features)

Input data.

yIgnored

Not used, present for API consistency by convention.

**kwargsdict

Arguments to be passed tofit.

Added in version 1.4.

Returns:
labelsndarray of shape (n_samples,), dtype=np.int64

Cluster labels.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please checkUser Guide on how the routingmechanism works.

Returns:
routingMetadataRequest

AMetadataRequest encapsulatingrouting information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator andcontained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects(such asPipeline). The latter haveparameters of the form<component>__<parameter> so that it’spossible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

Gallery examples#

Comparing different clustering algorithms on toy datasets

Comparing different clustering algorithms on toy datasets

Demo of OPTICS clustering algorithm

Demo of OPTICS clustering algorithm