无监督学习K-means聚类算法笔记-scikit-learn

大家早安、午安、晚安啦,今天继续学习scikit-learn中K-means聚类模型。在scikit-learn 中聚类的模型很多,可以见下面截图:

《无监督学习K-means聚类算法笔记-scikit-learn》 图1

而上述这些算法的差异性见下图:

《无监督学习K-means聚类算法笔记-scikit-learn》 图2

感觉好复杂的样子,辣么,先学K-means好啦,貌似是最简单的聚类。

在scikit-learn中,k-means算法是基于KMeans模型来实现,其基本的思想还是利用上一篇无监督学习K-means聚类算法笔记-Python中提到的最小化SSE(误差平方和)来逐步迭代求解质心,将数据分为不同的簇。

《无监督学习K-means聚类算法笔记-scikit-learn》 图3

上面提到的Inertia就是SSE。K-means方法的主要缺陷如下:

1)Inertia(SSE)其实是假设簇是具有凸的且同极性的(因为他是最小化与质心的距离),但是事实不一定是这样的,因此,当遇到分布式狭长的或者具有很多小分支的不规则分布的数据(It responds poorly to elongated clusters, or manifolds with irregular shapes.)时,该聚类方法的错误率就提高了,比如下图中的分类

《无监督学习K-means聚类算法笔记-scikit-learn》 图4

2)Inertia(SSE)并不是一个标准化的指标,我们只知道这个数值是越小越好且如果为0是最优的,但是在高维度特征值的数据集中,在计算欧式距离时,因为维度很高,导致距离公式急速膨胀,出现所谓的高维灾难。此时,就需要先用一些方法降维,然后再采用Kmeans算法。

具体来看看KMeans模型

class sklearn.cluster.KMeans(n_clusters=8, init=’k-means++’, n_init=10, max_iter=300, tol=0.0001, precompute_distances=’auto’, verbose=0, random_state=None, copy_x=True, n_jobs=1, algorithm=’auto’)

n_clusters->最终要形成的簇的个数

init: {‘k-means++’, ‘random’ or an ndarray}->获取初始化质心的方法

Method for initialization, defaults to ‘k-means++’:

‘k-means++’ : selects initial cluster centers for k-mean clustering in a smart way to speed up convergence. See section Notes in k_init for more details.

‘random’: choose k observations (rows) at random from data for the initial centroids.

If an ndarray is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.

在scikit-learn中,有个栗子是对比‘init: {‘k-means++’, ‘random’ or an ndarray}’中,不同的获取初始质心的方法将会影响K-means方法的聚类效果。

《无监督学习K-means聚类算法笔记-scikit-learn》 图5
《无监督学习K-means聚类算法笔记-scikit-learn》 图6
《无监督学习K-means聚类算法笔记-scikit-learn》 图7

图6中代码的仿真图:

《无监督学习K-means聚类算法笔记-scikit-learn》 图8

图7中代码仿真图:

《无监督学习K-means聚类算法笔记-scikit-learn》 图9

scikit-learn中KMeans模型基本介绍到这里,希望对大家有所帮助,也请大牛不吝赐教!

    原文作者:keepStriving
    原文地址: https://www.jianshu.com/p/af2d3844e9ef
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞