计算 OPTICS 图#
- sklearn.cluster.compute_optics_graph(X, *, min_samples, max_eps, metric, p, metric_params, algorithm, leaf_size, n_jobs)[source]#
- 计算OPTICS可达性图。 - 更多信息请阅读 用户指南。 - 参数:
- X形状为 (n_samples, n_features) 的{ndarray, sparse matrix},如果 metric=’precomputed’ 则为 (n_samples, n_samples)
- 特征数组,或者如果 metric=’precomputed’ 则为样本间距离数组。 
- min_samplesint > 1 或 0 到 1 之间的浮点数
- 一个点被认为是核心点的邻域样本数。表示为绝对数或样本数的比例(四舍五入到至少2)。 
- max_eps浮点数,默认为 np.inf
- 两个样本之间被认为在一个样本邻域内的最大距离。默认为 - np.inf将识别所有尺度上的聚类;减小- max_eps将缩短运行时间。
- metricstr 或 callable,默认为’minkowski’
- 用于距离计算的度量。可以使用 scikit-learn 或 scipy.spatial.distance 中的任何度量。 - 如果 metric 是一个可调用函数,则它会在每一对实例(行)上调用,并记录结果值。可调用函数应接收两个数组作为输入并返回一个值,表示它们之间的距离。这适用于 Scipy 的度量,但效率低于将度量名称作为字符串传递。如果 metric 为“precomputed”,则假设 X 为距离矩阵且必须为方阵。 - metric 的有效值为: - 来自 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 中移除。
- p浮点数,默认为 2
- 来自 - pairwise_distances的 Minkowski 度量的参数。当 p = 1 时,这等效于使用 manhattan_distance (l1),而 p = 2 时则等效于 euclidean_distance (l2)。对于任意 p,使用 minkowski_distance (l_p)。
- metric_params字典,默认为 None
- 度量函数的其他关键字参数。 
- algorithm{'auto', 'ball_tree', 'kd_tree', 'brute'},默认为'auto'
- 用于计算最近邻的算法 - ‘ball_tree’ 将使用 - BallTree。
- ‘kd_tree’ 将使用 - KDTree。
- ‘brute’ 将使用蛮力搜索。 
- ‘auto’ 将尝试根据传递给 - fit方法的值确定最合适的算法。(默认)
 - 注意:对稀疏输入进行拟合将覆盖此参数的设置,使用蛮力。 
- leaf_size整数,默认为 30
- 传递给 - BallTree或- KDTree的叶子大小。这会影响构建和查询的速度,以及存储树所需的内存。最佳值取决于问题的性质。
- n_jobs整数,默认为 None
- 为邻居搜索运行的并行作业数。 - None表示 1,除非在- joblib.parallel_backend上下文中。- -1表示使用所有处理器。更多详情请见 词汇表。
 
- 返回:
- ordering_形状为 (n_samples,) 的数组
- 样本索引的聚类有序列表。 
- core_distances_形状为 (n_samples,) 的数组
- 每个样本成为核心点的距离,按对象顺序索引。永远不会成为核心点的点的距离为 inf。使用 - clust.core_distances_[clust.ordering_]按聚类顺序访问。
- reachability_形状为 (n_samples,) 的数组
- 每个样本的可达距离,按对象顺序索引。使用 - clust.reachability_[clust.ordering_]按聚类顺序访问。
- predecessor_形状为 (n_samples,) 的数组
- 从哪个点到达样本,按对象顺序索引。种子点的祖先为 -1。 
 
 - 参考文献 [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]) 
