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)[源代码]#
集聚聚集。
循环合并样本数据的成对集群;使用链接距离。
阅读更多的 User Guide .
- 参数:
- n_clustersint或无,默认=2
要查找的集群数量。必须
None
如果distance_threshold
不None
.- metric字符串或可调用,默认=“欧几里得”
Metric used to compute the linkage. Can be "euclidean", "l1", "l2", "manhattan", "cosine", or "precomputed". If linkage is "ward", only "euclidean" is accepted. If "precomputed", a distance matrix is needed as input for the fit method. If connectivity is None, linkage is "single" and affinity is not "precomputed" any valid pairwise distance metric can be assigned.
Added in version 1.2.
- memory具有joblib.内存接口的字符串或对象,默认=无
用于缓存树计算的输出。默认情况下,不进行缓存。如果给出了字符串,则它是缓存目录的路径。
- connectivity类数组、稀疏矩阵或可调用,默认=无
连接矩阵。按照给定的数据结构为每个样本定义邻近样本。这可以是连接性矩阵本身,也可以是将数据转换为连接性矩阵的可调用矩阵,例如从
kneighbors_graph
.默认值为None
即分层集群算法是非结构化的。对于使用的连通性矩阵的示例
kneighbors_graph
,看到了 有结构和不有结构的集聚 .- compute_full_tree“Auto”或布尔,默认=“Auto”
Stop early the construction of the tree at
n_clusters
. This is useful to decrease computation time if the number of clusters is not small compared to the number of samples. This option is useful only when specifying a connectivity matrix. Note also that when varying the number of clusters and using caching, it may be advantageous to compute the full tree. It must beTrue
ifdistance_threshold
is notNone
. By defaultcompute_full_tree
is "auto", which is equivalent toTrue
whendistance_threshold
is notNone
or thatn_clusters
is inferior to the maximum between 100 or0.02 * n_samples
. Otherwise, "auto" is equivalent toFalse
.- linkage' ward ',',' single '},默认=' ward '
使用哪个链接标准。联系标准确定观察集之间使用的距离。该算法将合并最小化该标准的成对集群。
“ward”最大限度地减少了正在合并的集群的方差。
“average”使用两个集合的每个观测的距离的平均值。
“完全”或“最大”联动使用两组所有观测之间的最大距离。
“single”使用两个集合的所有观察之间的最小距离。
Added in version 0.20: 添加了“单一”选项
例如比较不同的例子
linkage
标准请参考 在玩具数据集上比较不同的分层链接方法 .- distance_thresholdfloat,默认=无
链接距离阈值达到或超过该阈值将不会合并集群。如果不是
None
,n_clusters
必须None
和compute_full_tree
必须True
.Added in version 0.21.
- compute_distances布尔,默认=假
计算集群之间的距离,即使
distance_threshold
未使用。这可以用于进行树图可视化,但会带来计算和内存负担。Added in version 0.24.
有关树图可视化的示例,请参阅 图分层聚集树图 .
- 属性:
- n_clusters_int
算法找到的集群数量。如果
distance_threshold=None
,它将等于给定的n_clusters
.- labels_形状的nd数组(n_samples)
每个点的集群标签。
- n_leaves_int
分层树中的叶子数量。
- n_connected_components_int
图形中连接组件的估计数量。
Added in version 0.21:
n_connected_components_
添加以取代n_components_
.- n_features_in_int
期间看到的功能数量 fit .
Added in version 0.24.
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
- children_形状类似阵列(n_samples-1,2)
The children of each non-leaf node. Values less than
n_samples
correspond to leaves of the tree which are the original samples. A nodei
greater than or equal ton_samples
is a non-leaf node and has childrenchildren_[i - n_samples]
. Alternatively at the i-th iteration, children[i][0] and children[i][1] are merged to form noden_samples + i
.- distances_形状类似阵列(n_nodes-1,)
中相应位置的节点之间的距离
children_
.仅在以下情况下计算distance_threshold
使用或compute_distances
设置为True
.
参见
FeatureAgglomeration
聚集性聚集,但针对特征而不是样本。
ward_tree
具有病房联系的分层聚集。
示例
>>> 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])
- fit(X, y=None)[源代码]#
根据特征或距离矩阵匹配分层集群。
- 参数:
- X类似阵列、形状(n_samples,n_features)或 (n_samples,n_samples)
训练实例进行集群,或实例之间的距离,如果
metric='precomputed'
.- y忽视
未使用,此处列出是为了按照惯例实现API一致性。
- 返回:
- self对象
返回匹配的实例。
- fit_predict(X, y=None)[源代码]#
匹配并返回每个样本的集群分配的结果。
除了拟合之外,该方法还返回训练集中每个样本的聚类分配结果。
- 参数:
- X形状类似阵列(n_samples,n_features)或 (n_samples,n_samples)
训练实例进行集群,或实例之间的距离,如果
affinity='precomputed'
.- y忽视
未使用,此处列出是为了按照惯例实现API一致性。
- 返回:
- labels形状的nd数组(n_samples,)
集群标签。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。