AgglomerativeClustering#
- class sklearn.cluster.AgglomerativeClustering(n_clusters=2, *, metric='euclidean', memory=None, connectivity=None, compute_full_tree='auto', linkage='ward', distance_threshold=None, compute_distances=False)[source]#
Agglomerative Clustering(层次聚类)。
递归地合并样本数据的成对聚类;使用连接距离。
在 User Guide 中阅读更多内容。
- 参数:
- n_clustersint or None, default=2
要找到的聚类数量。如果
distance_threshold不为None,则必须为None。- metricstr or callable, default=”euclidean”
用于计算连接的度量。可以是 “euclidean”(欧几里得),“l1”,“l2”,“manhattan”(曼哈顿),“cosine”(余弦)或 “precomputed”(预计算)。如果 linkage 为 “ward”,则只接受 “euclidean”。如果为 “precomputed”,则 fit 方法需要距离矩阵作为输入。如果 connectivity 为 None,linkage 为 “single” 且 affinity 不是 “precomputed”,则可以分配任何有效的成对距离度量。
有关不同度量下凝聚聚类的示例,请参阅 Agglomerative clustering with different metrics。
1.2 版本新增。
- memorystr 或具有 joblib.Memory 接口的对象, 默认为 None
用于缓存树计算的输出。默认情况下,不进行缓存。如果给定字符串,则为缓存目录的路径。
- connectivityarray-like, sparse matrix, or callable, default=None
连接矩阵。根据给定的数据结构定义每个样本的邻近样本。这本身可以是一个连接矩阵,也可以是一个将数据转换为连接矩阵的可调用对象,例如从
kneighbors_graph派生而来。默认为None,即层次聚类算法是非结构化的。有关使用
kneighbors_graph的连接矩阵示例,请参阅 Hierarchical clustering with and without structure。- compute_full_tree‘auto’ or bool, default=’auto’
在达到
n_clusters时提前停止树的构建。如果聚类数量相对于样本数量较小,这对于减少计算时间很有用。此选项仅在指定连接矩阵时有用。另请注意,当改变聚类数量并使用缓存时,计算完整的树可能更有优势。如果distance_threshold不为None,则必须为True。默认情况下,compute_full_tree为 “auto”,这等效于True当distance_threshold不为None或者n_clusters小于 100 和0.02 * n_samples中的较大值时。否则,“auto” 等效于False。- linkage{‘ward’, ‘complete’, ‘average’, ‘single’}, default=’ward’
要使用的连接标准。连接标准决定了在观察集之间使用的距离。算法将合并使此标准最小化的聚类对。
‘ward’ 最小化合并的聚类的方差。
‘average’ 使用两个集中每个观察值之间距离的平均值。
‘complete’ 或 ‘maximum’ 连接使用两个集中所有观察值之间的最大距离。
‘single’ 使用两个集中所有观察值之间的最小距离。
版本 0.20 中新增: 新增了 ‘single’ 选项
有关比较不同
linkage标准的示例,请参阅 Comparing different hierarchical linkage methods on toy datasets。- distance_thresholdfloat, default=None
聚类将不会合并的连接距离阈值或高于此阈值的聚类。如果不为
None,则n_clusters必须为None,且compute_full_tree必须为True。0.21 版本新增。
- compute_distancesbool, default=False
即使未使用
distance_threshold,也会计算聚类之间的距离。这可用于树状图可视化,但会引入计算和内存开销。0.24 版本新增。
有关树状图可视化的示例,请参阅 Plot Hierarchical Clustering Dendrogram。
- 属性:
- n_clusters_int
算法找到的聚类数量。如果
distance_threshold=None,则等于给定的n_clusters。- labels_ndarray of shape (n_samples)
每个点的聚类标签。
- n_leaves_int
层次树中的叶子数量。
- n_connected_components_int
图中连通分量的估计数量。
版本 0.21 中新增: 添加了
n_connected_components_以替换n_components_。- n_features_in_int
在 拟合 期间看到的特征数。
0.24 版本新增。
- feature_names_in_shape 为 (
n_features_in_,) 的 ndarray 在 fit 期间看到的特征名称。仅当
X具有全部为字符串的特征名称时才定义。1.0 版本新增。
- children_array-like of shape (n_samples-1, 2)
每个非叶节点的子节点。小于
n_samples的值对应于作为原始样本的树的叶子。大于或等于n_samples的节点i是一个非叶节点,其子节点为children_[i - n_samples]。或者在第 i 次迭代中,children[i][0]和children[i][1]合并形成节点n_samples + i。- distances_array-like of shape (n_nodes-1,)
在
children_中相应位置的节点之间的距离。仅在使用了distance_threshold或将compute_distances设置为True时计算。
另请参阅
FeatureAgglomeration凝聚聚类,但针对特征而非样本。
ward_tree使用 ward 连接的层次聚类。
示例
>>> from sklearn.cluster import AgglomerativeClustering >>> import numpy as np >>> X = np.array([[1, 2], [1, 4], [1, 0], ... [4, 2], [4, 4], [4, 0]]) >>> clustering = AgglomerativeClustering().fit(X) >>> clustering AgglomerativeClustering() >>> clustering.labels_ array([1, 1, 1, 0, 0, 0])
有关凝聚聚类与其他聚类算法的比较,请参阅 Comparing different clustering algorithms on toy datasets
- fit(X, y=None)[source]#
从特征或距离矩阵拟合层次聚类。
- 参数:
- Xarray-like, shape (n_samples, n_features) or (n_samples, n_samples)
要聚类的训练实例,如果
metric='precomputed',则为实例之间的距离。- y被忽略
Not used, present here for API consistency by convention.
- 返回:
- selfobject
返回已拟合的实例。
- fit_predict(X, y=None)[source]#
拟合并返回每个样本的聚类分配结果。
除了拟合之外,此方法还返回训练集中每个样本的聚类分配结果。
- 参数:
- X形状为 (n_samples, n_features) 或 (n_samples, n_samples) 的类数组对象
要聚类的训练实例,如果
affinity='precomputed',则为实例之间的距离。- y被忽略
Not used, present here for API consistency by convention.
- 返回:
- labelsndarray of shape (n_samples,)
聚类标签。
- get_metadata_routing()[source]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
- 返回:
- routingMetadataRequest
封装路由信息的
MetadataRequest。