大家早安、午安、晚安啦,今天繼續(xù)學(xué)習(xí)scikit-learn中K-means聚類模型。在scikit-learn 中聚類的模型很多,可以見(jiàn)下面截圖:

而上述這些算法的差異性見(jiàn)下圖:

感覺(jué)好復(fù)雜的樣子,辣么,先學(xué)K-means好啦,貌似是最簡(jiǎn)單的聚類。
在scikit-learn中,k-means算法是基于KMeans模型來(lái)實(shí)現(xiàn),其基本的思想還是利用上一篇無(wú)監(jiān)督學(xué)習(xí)K-means聚類算法筆記-Python中提到的最小化SSE(誤差平方和)來(lái)逐步迭代求解質(zhì)心,將數(shù)據(jù)分為不同的簇。

上面提到的Inertia就是SSE。K-means方法的主要缺陷如下:
1)Inertia(SSE)其實(shí)是假設(shè)簇是具有凸的且同極性的(因?yàn)樗亲钚』c質(zhì)心的距離),但是事實(shí)不一定是這樣的,因此,當(dāng)遇到分布式狹長(zhǎng)的或者具有很多小分支的不規(guī)則分布的數(shù)據(jù)(It responds poorly to elongated clusters, or manifolds with irregular shapes.)時(shí),該聚類方法的錯(cuò)誤率就提高了,比如下圖中的分類

2)Inertia(SSE)并不是一個(gè)標(biāo)準(zhǔn)化的指標(biāo),我們只知道這個(gè)數(shù)值是越小越好且如果為0是最優(yōu)的,但是在高維度特征值的數(shù)據(jù)集中,在計(jì)算歐式距離時(shí),因?yàn)榫S度很高,導(dǎo)致距離公式急速膨脹,出現(xiàn)所謂的高維災(zāi)難。此時(shí),就需要先用一些方法降維,然后再采用Kmeans算法。
具體來(lái)看看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->最終要形成的簇的個(gè)數(shù)
init: {‘k-means++’, ‘random’ or an ndarray}->獲取初始化質(zhì)心的方法
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中,有個(gè)栗子是對(duì)比‘init: {‘k-means++’, ‘random’ or an ndarray}’中,不同的獲取初始質(zhì)心的方法將會(huì)影響K-means方法的聚類效果。



圖6中代碼的仿真圖:

圖7中代碼仿真圖:

scikit-learn中KMeans模型基本介紹到這里,希望對(duì)大家有所幫助,也請(qǐng)大牛不吝賜教!