热门搜索 :
考研考公
您的当前位置:首页正文

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

来源:东饰资讯网
图1

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

图2

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

图3

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

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

图4

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

图5 图6 图7

图6中代码的仿真图:

图8

图7中代码仿真图:

图9
Top