本文主要是介绍【Scikit-Learn 中文文档】21 聚类 - 无监督学习 - 用户指南 | ApacheCN,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
中文文档: http://sklearn.apachecn.org/cn/stable/modules/clustering.html
英文文档: http://sklearn.apachecn.org/en/stable/modules/clustering.html
官方文档: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个 Star,我们一直在努力)
贡献者: https://github.com/apachecn/scikit-learn-doc-zh#贡献者
关于我们: http://www.apachecn.org/organization/209.html
2.3. 聚类
未标记的数据的 Clustering(聚类) 可以使用模块 sklearn.cluster
来实现。
每个 clustering algorithm (聚类算法)有两个变体: 一个是 class, 它实现了 fit
方法来学习 train data(训练数据)的 clusters(聚类),还有一个 function(函数),是给定 train data(训练数据),返回与不同 clusters(聚类)对应的整数标签 array(数组)。对于 class(类),training data(训练数据)上的标签可以在 labels_
属性中找到。
输入数据
需要注意的一点是,该模块中实现的算法可以采用不同种类的 matrix (矩阵)作为输入。所有这些都接受 shape [n_samples, n_features]
的标准数据矩阵。 这些可以从以下的 sklearn.feature_extraction
模块的 classes (类)中获得。对于 AffinityPropagation
, SpectralClustering
和 DBSCAN
也可以输入 shape [n_samples, n_samples]
的相似矩阵。这些可以从 sklearn.metrics.pairwise
模块中的函数获得。
2.3.1. 聚类方法概述
Method name(方法名称) | Parameters(参数) | Scalability(可扩展性) | Usecase(使用场景) | Geometry (metric used)(几何图形(公制使用)) |
---|---|---|---|---|
K-Means(K-均值) | number of clusters(聚类形成的簇的个数) | 非常大的 n_samples , 中等的 n_clusters 使用 MiniBatch code(MiniBatch 代码) | 通用, 均匀的 cluster size(簇大小), flat geometry(平面几何), 不是太多的 clusters(簇) | Distances between points(点之间的距离) |
Affinity propagation | damping(阻尼), sample preference(样本偏好) | Not scalable with n_samples(n_samples 不可扩展) | Many clusters, uneven cluster size, non-flat geometry(许多簇,不均匀的簇大小,非平面几何) | Graph distance (e.g. nearest-neighbor graph)(图形距离(例如,最近邻图)) |
Mean-shift | bandwidth(带宽) | Not scalable with n_samples (不可扩展的 n_samples ) | Many clusters, uneven cluster size, non-flat geometry(许多簇,不均匀的簇大小,非平面几何) | Distances between points(点之间的距离) |
Spectral clustering | number of clusters(簇的个数) | 中等的 n_samples , 小的 n_clusters | Few clusters, even cluster size, non-flat geometry(几个簇,均匀的簇大小,非平面几何) | Graph distance (e.g. nearest-neighbor graph)(图形距离(例如最近邻图)) |
Ward hierarchical clustering | number of clusters(簇的个数) | 大的 n_samples 和 n_clusters | Many clusters, possibly connectivity constraints(很多的簇,可能连接限制) | Distances between points(点之间的距离) |
Agglomerative clustering | number of clusters(簇的个数), linkage type(链接类型), distance(距离) | 大的 n_samples 和 n_clusters | Many clusters, possibly connectivity constraints, non Euclidean distances(很多簇,可能连接限制,非欧几里得距离) | Any pairwise distance(任意成对距离) |
DBSCAN | neighborhood size(neighborhood 的大小) | 非常大的 n_samples , 中等的 n_clusters | Non-flat geometry, uneven cluster sizes(非平面几何,不均匀的簇大小) | Distances between nearest points(最近点之间的距离) |
Gaussian mixtures(高斯混合) | many(很多) | Not scalable(不可扩展) | Flat geometry, good for density estimation(平面几何,适用于密度估计) | Mahalanobis distances to centers(Mahalanobis 与中心的距离) |
Birch | branching factor(分支因子), threshold(阈值), optional global clusterer(可选全局簇). | 大的 n_clusters 和 n_samples | Large dataset, outlier removal, data reduction.(大数据集,异常值去除,数据简化) | Euclidean distance between points(点之间的欧式距离) |
当 clusters (簇)具有 specific shape (特殊的形状),即 non-flat manifold(非平面 manifold),并且标准欧几里得距离不是正确的 metric (度量标准)时,Non-flat geometry clustering (非平面几何聚类)是非常有用的。这种情况出现在上图的两个顶行中。
用于 clustering (聚类)的 Gaussian mixture models (高斯混合模型),专用于 mixture models (混合模型)描述在 文档的另一章节 。可以将 KMeans 视为具有 equal covariance per component (每个分量相等协方差)的 Gaussian mixture model (高斯混合模型)的特殊情况。
KMeans
算法通过试图分离 n groups of equal variance(n 个相等方差组)的样本来聚集数据,minimizing (最小化)称为 inertia 或者 within-cluster sum-of-squares (簇内和平方)的 criterion (标准)。 该算法需要指定 number of clusters (簇的数量)。它可以很好地 scales (扩展)到 large number of samples(大量样本),并已经被广泛应用于许多不同领域的应用领域。
k-means 算法将一组 样本
划分成
不相交的 clusters (簇)
, 每个都用 cluster (该簇)中的样本的均值
描述。 这个 means (均值)通常被称为 cluster(簇)的 “centroids(质心)”; 注意,它们一般不是从
中挑选出的点,虽然它们是处在同一个 space(空间)。 K-means(K-均值)算法旨在选择最小化 inertia(惯性) 或 within-cluster sum of squared(簇内和的平方和)的标准的 centroids(质心):
Inertia(惯性), 或 the within-cluster sum of squares(簇内和平方差) criterion(标准),可以被认为是 internally coherent clusters (内部想干聚类)的 measure (度量)。 它有各种缺点:
- Inertia(惯性)假设 clusters (簇)是 convex(凸)的和 isotropic (各项同性),这并不是总是这样。它对 elongated clusters (细长的簇)或具有不规则形状的 manifolds 反应不佳。
- Inertia(惯性)不是一个 normalized metric(归一化度量): 我们只知道 lower values (较低的值)是更好的,并且 零 是最优的。但是在 very high-dimensional spaces (非常高维的空间)中,欧几里得距离往往会变得 inflated (膨胀)(这就是所谓的 “curse of dimensionality (维度诅咒/维度惩罚)”)。在 k-means 聚类之前运行诸如 PCA 之类的 dimensionality reduction algorithm (降维算法)可以减轻这个问题并加快计算速度。

K-means 通常被称为 Lloyd’s algorithm(劳埃德算法)。在基本术语中,算法有三个步骤。、 第一步是选择 initial centroids (初始质心),最基本的方法是从 数据集中选择
个样本。初始化完成后,K-means 由两个其他步骤之间的循环组成。 第一步将每个样本分配到其 nearest centroid (最近的质心)。第二步通过取分配给每个先前质心的所有样本的平均值来创建新的质心。计算旧的和新的质心之间的差异,并且算法重复这些最后的两个步骤,直到该值小于阈值。换句话说,算法重复这个步骤,直到质心不再显著移动。

K-means 相当于具有 small, all-equal, diagonal covariance matrix (小的全对称协方差矩阵)的 expectation-maximization algorithm (期望最大化算法)。
该算法也可以通过 `Voronoi diagrams(Voronoi图)<https://en.wikipedia.org/wiki/Voronoi_diagram>`_ 的概念来理解。首先使用 current centroids (当前质心)计算点的 Voronoi 图。 Voronoi 图中的每个 segment (段)都成为一个 separate cluster (单独的簇)。其次,centroids(质心)被更新为每个 segment (段)的 mean(平均值)。然后,该算法重复此操作,直到满足停止条件。 通常情况下,当 iterations (迭代)之间的 objective function (目标函数)的相对减小小于给定的 tolerance value (公差值)时,算法停止。在此实现中不是这样: 当质心移动小于 tolerance (公差)时,迭代停止。
给定足够的时间,K-means 将总是收敛的,但这可能是 local minimum (局部最小)的。这很大程度上取决于 initialization of the centroids (质心的初始化)。 因此,通常会进行几次 different initializations of the centroids (初始化不同质心)的计算。帮助解决这个问题的一种方法是 k-means++ 初始化方案,它已经在 scikit-learn 中实现(使用 init='k-means++'
参数)。 这将初始化 centroids (质心)(通常)彼此远离,导致比随机初始化更好的结果,如参考文献所示。
可以给出一个参数,以允许 K-means 并行运行,称为 n_jobs
。给这个参数一个正值使用许多处理器(默认值: 1)。值 -1 使用所有可用的处理器,-2 使用一个,等等。Parallelization (并行化)通常以 cost of memory(内存的代价)加速计算(在这种情况下,需要存储多个质心副本,每个作业使用一个)。
Warning
当 numpy 使用 Accelerate 框架时,K-Means 的并行版本在 OS X 上损坏。这是 expected behavior (预期的行为): Accelerate 可以在 fork 之后调用,但是您需要使用 Python binary(二进制)(该多进程在 posix 下不执行)来执行子进程。
K-means 可用于 vector quantization (矢量量化)。这是使用以下类型的 trained model (训练模型)的变换方法实现的 KMeans
。
示例:
- Demonstration of k-means assumptions: 演示 k-means 是否 performs intuitively (直观执行),何时不执行
- A demo of K-Means clustering on the handwritten digits data: 聚类手写数字
参考:
- “k-means++: The advantages of careful seeding” Arthur, David, and Sergei Vassilvitskii, Proceedings of the eighteenth annual ACM-SIAM symposium on Discrete algorithms, Society for Industrial and Applied Mathematics (2007)
2.3.2.1. 小批量 K-Means
2.3.3. Affinity Propagation
2.3.4. Mean Shift
2.3.5. Spectral clustering
2.3.5.1. 不同的标记分配策略
assign_labels="kmeans" | assign_labels="discretize" |
---|---|
![]() | ![]() |
参考:
- “A Tutorial on Spectral Clustering” Ulrike von Luxburg, 2007
- “Normalized cuts and image segmentation” Jianbo Shi, Jitendra Malik, 2000
- “A Random Walks View of Spectral Segmentation” Marina Meila, Jianbo Shi, 2001
- “On Spectral Clustering: Analysis and an algorithm” Andrew Y. Ng, Michael I. Jordan, Yair Weiss, 2001
2.3.6. 层次聚类
2.3.6.1. Different linkage type: Ward, complete and average linkage
2.3.6.2. 添加连接约束
2.3.6.3. Varying the metric
2.3.7. DBSCAN
2.3.8. Birch
2.3.9. 聚类性能度量
2.3.9.1. 调整后的 Rand 指数
2.3.9.1.1. 优点
2.3.9.1.2. 缺点
2.3.9.1.3. 数学表达
2.3.9.2. 基于 Mutual Information (互信息)的分数
2.3.9.2.1. 优点
2.3.9.2.2. 缺点
2.3.9.2.3. 数学公式
2.3.9.3. 同质性,完整性和 V-measure
2.3.9.3.1. 优点
2.3.9.3.2. 缺点
2.3.9.3.3. 数学表达
2.3.9.4. Fowlkes-Mallows 分数
2.3.9.4.1. 优点
2.3.9.4.2. 缺点
2.3.9.5. Silhouette 系数
2.3.9.5.1. 优点
2.3.9.5.2. 缺点
2.3.9.6. Calinski-Harabaz 指数
2.3.9.6.1. 优点
2.3.9.6.2. 缺点
官方文档: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个 Star,我们一直在努力)
贡献者: https://github.com/apachecn/scikit-learn-doc-zh#贡献者
关于我们: http://www.apachecn.org/organization/209.html
有兴趣的们也可以和我们一起来维护,持续更新中 。。。
机器学习交流群: 629470233
这篇关于【Scikit-Learn 中文文档】21 聚类 - 无监督学习 - 用户指南 | ApacheCN的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!