compute_optics_graph#
- sklearn.cluster.compute_optics_graph(X, *, min_samples, max_eps, metric, p, metric_params, algorithm, leaf_size, n_jobs)[source]#
计算 OPTICS 可达性图。
在用户指南中阅读更多内容。
- 参数:
- X{ndarray, sparse matrix} of shape (n_samples, n_features), or (n_samples, n_samples) if metric=’precomputed’
特征数组,如果 metric=’precomputed’,则是样本之间的距离数组。
- min_samplesint > 1 or float between 0 and 1
一个点被视为核心点所需的邻域中的样本数。表示为绝对数字或样本数的一部分(四舍五入后至少为2)。
- max_epsfloat, default=np.inf
两个样本之间被视为彼此邻域中的最大距离。默认值
np.inf将识别所有尺度的簇;减小max_eps将导致更短的运行时间。- metricstr or callable, default=’minkowski’
用于距离计算的度量。可以使用 scikit-learn 或 scipy.spatial.distance 中的任何度量。
如果 metric 是一个可调用函数,它将作用于每对实例(行)并记录结果值。该可调用函数应接受两个数组作为输入,并返回一个表示它们之间距离的值。这适用于 Scipy 的度量,但不如将度量名称作为字符串传递高效。如果 metric 是“precomputed”,则假定 X 是一个距离矩阵,并且必须是方形的。
度量的有效值包括:
来自 scikit-learn:[‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’]
来自 scipy.spatial.distance:[‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’]
有关这些度量的详细信息,请参阅 scipy.spatial.distance 的文档。
注意
'kulsinski'在 SciPy 1.9 中已弃用,并将在 SciPy 1.11 中移除。- pfloat, default=2
来自
pairwise_distances的 Minkowski 度量的参数。当 p = 1 时,这相当于使用 manhattan_distance (l1),p = 2 时相当于 euclidean_distance (l2)。对于任意 p,使用 minkowski_distance (l_p)。- metric_paramsdict, default=None
度量函数的附加关键字参数。
- algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’
用于计算最近邻居的算法
‘ball_tree’ 将使用
BallTree。‘kd_tree’ 将使用
KDTree。'brute' 将使用暴力搜索。
‘auto’ 将尝试根据传递给
fit方法的值来决定最合适的算法。(默认值)
注意:对稀疏输入进行拟合将覆盖此参数的设置,使用暴力方法。
- leaf_sizeint, default=30
传递给
BallTree或KDTree的叶大小。这会影响构建和查询的速度,以及存储树所需的内存。最佳值取决于问题的性质。- n_jobsint, default=None
用于邻居搜索的并行作业数。
None表示 1,除非在joblib.parallel_backend上下文中。-1表示使用所有处理器。有关详细信息,请参阅 词汇表。
- 返回:
- ordering_array of shape (n_samples,)
按簇顺序排列的样本索引列表。
- core_distances_array of shape (n_samples,)
每个样本成为核心点的距离,按对象顺序索引。永远不会成为核心点的点距离为 inf。使用
clust.core_distances_[clust.ordering_]按簇顺序访问。- reachability_array of shape (n_samples,)
每个样本的可达性距离,按对象顺序索引。使用
clust.reachability_[clust.ordering_]按簇顺序访问。- predecessor_array of shape (n_samples,)
样本被到达时所来自的点,按对象顺序索引。种子点的前驱为 -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.
示例
>>> 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])