FeatureAgglomeration#
- class sklearn.cluster.FeatureAgglomeration(n_clusters=2, *, metric='euclidean', memory=None, connectivity=None, compute_full_tree='auto', linkage='ward', pooling_func=<function mean>, distance_threshold=None, compute_distances=False)[源代码]#
聚集特征。
循环合并一对特征集群。
参阅 特征聚集与单变量选择 例如比较
FeatureAgglomeration
具有单变量特征选择策略(基于方差分析)的策略。阅读更多的 User Guide .
- 参数:
- n_clustersint或无,默认=2
要查找的集群数量。必须
None
如果distance_threshold
不None
.- metric字符串或可调用,默认=“欧几里得”
用于计算联动的指标。可以是“欧几里得”、“l1”、“l2”、“曼哈顿”、“cos”或“预先计算的”。如果链接是“沃德”,则仅接受“欧几里得”。如果“预先计算”,则需要距离矩阵作为匹配方法的输入。
Added in version 1.2.
- memory具有joblib.内存接口的字符串或对象,默认=无
用于缓存树计算的输出。默认情况下,不进行缓存。如果给出了字符串,则它是缓存目录的路径。
- connectivity类数组、稀疏矩阵或可调用,默认=无
连接矩阵。按照给定的数据结构为每个要素定义邻近要素。这可以是连接性矩阵本身,也可以是将数据转换为连接性矩阵的可调用矩阵,例如从
kneighbors_graph
.默认值为None
即分层集群算法是非结构化的。- 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 features. 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”,“complete”,“average”,“single”},default=“ward”
使用哪个链接标准。链接标准确定要素集之间使用的距离。该算法将合并最小化该标准的成对集群。
“ward”最大限度地减少了正在合并的集群的方差。
“完全”或最大联动使用两组所有特征之间的最大距离。
“平均”使用两个集合中每个特征的距离的平均值。
“单一”使用两组所有特征之间的最小距离。
- pooling_func可调用,默认=NP.mean
这将聚集要素的值组合为单个值,并且应该接受一系列形状 [M, N] 以及关键字参数
axis=1
,并将其缩减为大小为 [M] .- 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_类数组的(n_features,)
每个要素的集群标签。
- 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_nodes-1,2)
The children of each non-leaf node. Values less than
n_features
correspond to leaves of the tree which are the original samples. A nodei
greater than or equal ton_features
is a non-leaf node and has childrenchildren_[i - n_features]
. Alternatively at the i-th iteration, children[i][0] and children[i][1] are merged to form noden_features + i
.- distances_形状类似阵列(n_nodes-1,)
中相应位置的节点之间的距离
children_
.仅在以下情况下计算distance_threshold
使用或compute_distances
设置为True
.
参见
AgglomerativeClustering
聚集性聚集样本而不是特征。
ward_tree
具有病房联系的分层聚集。
示例
>>> import numpy as np >>> from sklearn import datasets, cluster >>> digits = datasets.load_digits() >>> images = digits.images >>> X = np.reshape(images, (len(images), -1)) >>> agglo = cluster.FeatureAgglomeration(n_clusters=32) >>> agglo.fit(X) FeatureAgglomeration(n_clusters=32) >>> X_reduced = agglo.transform(X) >>> X_reduced.shape (1797, 32)
- fit(X, y=None)[源代码]#
将分层聚集与数据相匹配。
- 参数:
- X形状类似阵列(n_samples,n_features)
数据。
- y忽视
未使用,此处列出是为了按照惯例实现API一致性。
- 返回:
- self对象
返回Transformer。
- property fit_predict#
匹配并返回每个样本的集群分配的结果。
- fit_transform(X, y=None, **fit_params)[源代码]#
适应数据,然后对其进行转换。
适合变压器
X
和y
具有可选参数fit_params
并返回的转换版本X
.- 参数:
- X形状类似阵列(n_samples,n_features)
输入样本。
- y形状为(n_samples,)或(n_samples,n_outputs)的阵列状, 默认值=无
目标值(无监督转换)。
- **fit_paramsdict
其他适合参数。
- 返回:
- X_newndray形状数组(n_samples,n_features_new)
变形的数组。
- get_feature_names_out(input_features=None)[源代码]#
获取用于转换的输出要素名称。
输出的功能名称将以大写的类别名称为开头。例如,如果Transformer输出3个特征,则输出的特征名称为:
["class_name0", "class_name1", "class_name2"]
.- 参数:
- input_features字符串或无的类数组,默认=无
仅用于通过中看到的名称验证要素名称
fit
.
- 返回:
- feature_names_out字符串对象的nd数组
转换的功能名称。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- inverse_transform(X=None, *, Xt=None)[源代码]#
逆变换并返回大小的载体
n_features
.- 参数:
- X形状类似阵列(n_samples,n_clusters)或(n_clusters,)
要分配给每个样本集群的值。
- Xt形状类似阵列(n_samples,n_clusters)或(n_clusters,)
要分配给每个样本集群的值。
自 1.5 版本弃用:
Xt
在1.5中已废弃,并将在1.7中删除。使用X
而不是.
- 返回:
- X形状的nd数组(n_samples,n_features)或(n_features,)
大小的载体
n_samples
的价值观Xred
分配给每个样本集群。
- set_output(*, transform=None)[源代码]#
设置输出容器。
看到 介绍 set_output API 了解如何使用API的示例。
- 参数:
- transform{“默认”,“pandas”,“polars”},默认=无
配置输出
transform
和fit_transform
."default"
:Transformer的默认输出格式"pandas"
:DataFrame输出"polars"
:两极输出None
:转换配置不变
Added in version 1.4:
"polars"
添加了选项。
- 返回:
- self估计器实例
估计实例。