compute_optics_graph (original) (raw)

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 the User 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 if metric=’precomputed’.

min_samplesint > 1 or float between 0 and 1

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

max_epsfloat, default=np.inf

The maximum distance between two samples for one to be considered as in the neighborhood of the other. Default value of np.inf will identify clusters across all scales; reducing max_eps will result in shorter run times.

metricstr or callable, default=’minkowski’

Metric to use for distance computation. Any metric from scikit-learn 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. If metric is “precomputed”, X is assumed to be a distance matrix and must be square.

Valid values for metric are:

See the documentation for scipy.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 is equivalent 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:

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

leaf_sizeint, 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_jobsint, default=None

The number of parallel jobs to run for neighbors search.None means 1 unless in a joblib.parallel_backend context.-1 means using all processors. See Glossaryfor 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 object order. 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 clustering structure.” ACM SIGMOD Record 28, no. 2 (1999): 49-60.

Examples

import numpy as np from sklearn.cluster import compute_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, ... ) ordering array([0, 1, 2, 5, 3, 4]) core_distances array([3.16..., 1.41..., 1.41..., 1. , 1. , 4.12...]) reachability array([ inf, 3.16..., 1.41..., 4.12..., 1. , 5. ]) predecessor array([-1, 0, 1, 5, 3, 2])