compute_optics_graph#

sklearn.cluster.compute_optics_graph(X,*,min_samples,max_eps,metric,p,metric_params,algorithm,leaf_size,n_jobs)[source]#

Compute the OPTICS reachability graph.

Read more in theUser Guide.

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’.

min_samplesint > 1 or float between 0 and 1

The number of samples in a neighborhood for a point to be consideredas a core point. Expressed as an absolute number or a fraction of thenumber of samples (rounded to be at least 2).

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-learnor scipy.spatial.distance can be used.

If metric 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 be square.

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’]

See the documentation for scipy.spatial.distance for details on thesemetrics.

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.

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’ will attempt to decide the most appropriate algorithmbased on the values passed tofit method. (default)

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.

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.

Returns:
ordering_array of shape (n_samples,)

The cluster ordered list of sample indices.

core_distances_array 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.

reachability_array of shape (n_samples,)

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

predecessor_array of shape (n_samples,)

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

References

[1]

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.

Examples

>>>importnumpyasnp>>>fromsklearn.clusterimportcompute_optics_graph>>>X=np.array([[1,2],[2,5],[3,6],...[8,7],[8,8],[7,3]])>>>ordering,core_distances,reachability,predecessor=compute_optics_graph(...X,...min_samples=2,...max_eps=np.inf,...metric="minkowski",...p=2,...metric_params=None,...algorithm="auto",...leaf_size=30,...n_jobs=None,...)>>>orderingarray([0, 1, 2, 5, 3, 4])>>>core_distancesarray([3.16, 1.41, 1.41, 1.        , 1.        ,       4.12])>>>reachabilityarray([       inf, 3.16, 1.41, 4.12, 1.        ,       5.        ])>>>predecessorarray([-1,  0,  1,  5,  3,  2])

This Page