sklearn.cluster
.OPTICS¶

class
sklearn.cluster.
OPTICS
(min_samples=5, max_eps=inf, metric=’minkowski’, p=2, metric_params=None, maxima_ratio=0.75, rejection_ratio=0.7, similarity_threshold=0.4, significant_min=0.003, min_cluster_size=0.005, min_maxima_ratio=0.001, algorithm=’auto’, leaf_size=30, n_jobs=None)[source]¶ Estimate clustering structure from vector array
OPTICS: Ordering Points To Identify the Clustering Structure Closely related to DBSCAN, finds core sample of high density and expands clusters from them. Unlike DBSCAN, keeps cluster hierarchy for a variable neighborhood radius. Better suited for usage on large point datasets than the current sklearn implementation of DBSCAN.
This implementation deviates from the original OPTICS by first performing knearestneighborhood searches on all points to identify core sizes, then computing only the distances to unprocessed points when constructing the cluster order. Note that we do not employ a heap to manage the expansion candidates, so the time complexity will be O(n^2).
Read more in the User Guide.
Parameters:  min_samples : int (default=5)
The number of samples in a neighborhood for a point to be considered as a core point.
 max_eps : float, optional (default=np.inf)
The maximum distance between two samples for them to be considered as in the same neighborhood. Default value of “np.inf” will identify clusters across all scales; reducing
max_eps
will result in shorter run times. metric : string or callable, optional (default=’minkowski’)
metric to use for distance computation. Any metric from scikitlearn or scipy.spatial.distance can be used.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Distance matrices are not supported.
Valid values for metric are:
 from scikitlearn: [‘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’]
See the documentation for scipy.spatial.distance for details on these metrics.
 p : integer, optional (default=2)
Parameter for the Minkowski metric from
sklearn.metrics.pairwise_distances
. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used. metric_params : dict, optional (default=None)
Additional keyword arguments for the metric function.
 maxima_ratio : float, optional (default=.75)
The maximum ratio we allow of average height of clusters on the right and left to the local maxima in question. The higher the ratio, the more generous the algorithm is to preserving local minima, and the more cuts the resulting tree will have.
 rejection_ratio : float, optional (default=.7)
Adjusts the fitness of the clustering. When the maxima_ratio is exceeded, determine which of the clusters to the left and right to reject based on rejection_ratio. Higher values will result in points being more readily classified as noise; conversely, lower values will result in more points being clustered.
 similarity_threshold : float, optional (default=.4)
Used to check if nodes can be moved up one level, that is, if the new cluster created is too “similar” to its parent, given the similarity threshold. Similarity can be determined by 1) the size of the new cluster relative to the size of the parent node or 2) the average of the reachability values of the new cluster relative to the average of the reachability values of the parent node. A lower value for the similarity threshold means less levels in the tree.
 significant_min : float, optional (default=.003)
Sets a lower threshold on how small a significant maxima can be.
 min_cluster_size : int > 1 or float between 0 and 1 (default=0.005)
Minimum number of samples in an OPTICS cluster, expressed as an absolute number or a fraction of the number of samples (rounded to be at least 2).
 min_maxima_ratio : float, optional (default=.001)
Used to determine neighborhood size for minimum cluster membership. Each local maxima should be a largest value in a neighborhood of the
size min_maxima_ratio * len(X)
from left and right. algorithm : {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional
Algorithm used to compute the nearest neighbors:
 ‘ball_tree’ will use
BallTree
 ‘kd_tree’ will use
KDTree
 ‘brute’ will use a bruteforce search.
 ‘auto’ will attempt to decide the most appropriate algorithm
based on the values passed to
fit
method. (default)
Note: fitting on sparse input will override the setting of this parameter, using brute force.
 ‘ball_tree’ will use
 leaf_size : int, optional (default=30)
Leaf size passed to
BallTree
orKDTree
. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem. n_jobs : int or None, optional (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. See Glossary for more details.
Attributes:  core_sample_indices_ : array, shape (n_core_samples,)
Indices of core samples.
 labels_ : array, shape (n_samples,)
Cluster labels for each point in the dataset given to fit(). Noisy samples are given the label 1.
 reachability_ : array, shape (n_samples,)
Reachability distances per sample, indexed by object order. Use
clust.reachability_[clust.ordering_]
to access in cluster order. ordering_ : array, shape (n_samples,)
The cluster ordered list of sample indices.
 core_distances_ : array, shape (n_samples,)
Distance at which each sample becomes a core point, indexed by object order. Points which will never be core have a distance of inf. Use
clust.core_distances_[clust.ordering_]
to access in cluster order. predecessor_ : array, shape (n_samples,)
Point that a sample was reached from, indexed by object order. Seed points have a predecessor of 1.
See also
DBSCAN
 A similar clustering for a specified neighborhood radius (eps). Our implementation is optimized for runtime.
References
Ankerst, Mihael, Markus M. Breunig, HansPeter Kriegel, and Jörg Sander. “OPTICS: ordering points to identify the clustering structure.” ACM SIGMOD Record 28, no. 2 (1999): 4960.
Schubert, Erich, Michael Gertz. “Improving the Cluster Structure Extracted from OPTICS Plots.” Proc. of the Conference “Lernen, Wissen, Daten, Analysen” (LWDA) (2018): 318329.
Methods
extract_dbscan
(eps)Performs DBSCAN extraction for an arbitrary epsilon. fit
(X[, y])Perform OPTICS clustering fit_predict
(X[, y])Performs clustering on X and returns cluster labels. get_params
([deep])Get parameters for this estimator. set_params
(**params)Set the parameters of this estimator. 
__init__
(min_samples=5, max_eps=inf, metric=’minkowski’, p=2, metric_params=None, maxima_ratio=0.75, rejection_ratio=0.7, similarity_threshold=0.4, significant_min=0.003, min_cluster_size=0.005, min_maxima_ratio=0.001, algorithm=’auto’, leaf_size=30, n_jobs=None)[source]¶

extract_dbscan
(eps)[source]¶ Performs DBSCAN extraction for an arbitrary epsilon.
Extraction runs in linear time. Note that if the
max_eps
OPTICS parameter was set to < inf for extracting reachability and ordering arrays, DBSCAN extractions will be unstable foreps
values close tomax_eps
. Settingeps
< (max_eps
/ 5.0) will guarantee extraction parity with DBSCAN.Parameters: Returns:  core_sample_indices_ : array, shape (n_core_samples,)
The indices of the core samples.
 labels_ : array, shape (n_samples,)
The estimated labels.

fit
(X, y=None)[source]¶ Perform OPTICS clustering
Extracts an ordered list of points and reachability distances, and performs initial clustering using
max_eps
distance specified at OPTICS object instantiation.Parameters:  X : array, shape (n_samples, n_features)
The data.
 y : ignored
Returns:  self : instance of OPTICS
The instance.

fit_predict
(X, y=None)[source]¶ Performs clustering on X and returns cluster labels.
Parameters:  X : ndarray, shape (n_samples, n_features)
Input data.
 y : Ignored
not used, present for API consistency by convention.
Returns:  labels : ndarray, shape (n_samples,)
cluster labels

get_params
(deep=True)[source]¶ Get parameters for this estimator.
Parameters:  deep : boolean, optional
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns:  params : mapping of string to any
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 as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.Returns:  self