07 聚類演算法 - 程式碼案例三 - K-Means演算法和Mini Batch K-Means演算法效果評估

weixin_34390105發表於2018-12-08

03 聚類演算法 - K-means聚類
04 聚類演算法 - 程式碼案例一 - K-means聚類
05 聚類演算法 - 二分K-Means、K-Means++、K-Means||、Canopy、Mini Batch K-Means演算法
06 聚類演算法 - 程式碼案例二 - K-Means演算法和Mini Batch K-Means演算法比較

需求: 基於scikit包中的建立模擬資料的API建立聚類資料,對K-Means演算法和Mini Batch K-Means演算法構建的模型進行評估。

3153092-87bbb3ed293b2bef.png
相關API

常規操作:

import time
import numpy as np  
import matplotlib.pyplot as plt  
import matplotlib as mpl
from sklearn.cluster import MiniBatchKMeans, KMeans 
from sklearn import metrics
from sklearn.metrics.pairwise import pairwise_distances_argmin  
from sklearn.datasets.samples_generator import make_blobs  

## 設定屬性防止中文亂碼
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
一、構建資料
centers = [[1, 1], [-1, -1], [1, -1]] 
clusters = len(centers)       

X, Y = make_blobs(n_samples=3000, centers=centers, cluster_std=0.7, random_state=28) 
Y # 在實際工作中是人工給定的,專門用於判斷聚類的效果的一個值

array([2, 0, 0, ..., 2, 2, 1])


二、構建k-means++模型
k_means = KMeans(init='k-means++', n_clusters=clusters, random_state=28)
t0 = time.time() 
k_means.fit(X)  
km_batch = time.time() - t0  
print ("K-Means演算法模型訓練消耗時間:%.4fs" % km_batch)

K-Means演算法模型訓練消耗時間:0.1211s


三、構建Mini Batch K-Means模型
batch_size = 100
mbk = MiniBatchKMeans(init='k-means++', n_clusters=clusters, 
    batch_size=batch_size, random_state=28)  
t0 = time.time()  
mbk.fit(X)  
mbk_batch = time.time() - t0  
print ("Mini Batch K-Means演算法模型訓練消耗時間:%.4fs" % mbk_batch)

Mini Batch K-Means演算法模型訓練消耗時間:0.0991s


km_y_hat = k_means.labels_
mbkm_y_hat = mbk.labels_
print(km_y_hat) # 樣本所屬的類別

[0 2 2 ... 1 1 0]


k_means_cluster_centers = k_means.cluster_centers_
mbk_means_cluster_centers = mbk.cluster_centers_
print ("K-Means演算法聚類中心點:\ncenter=", k_means_cluster_centers)
print ("Mini Batch K-Means演算法聚類中心點:\ncenter=", mbk_means_cluster_centers)
order = pairwise_distances_argmin(k_means_cluster_centers,  
                                  mbk_means_cluster_centers) 
order

K-Means演算法聚類中心點:
center= [[-1.0600799 -1.05662982]
[ 1.02975208 -1.07435837]
[ 1.01491055 1.02216649]]
Mini Batch K-Means演算法聚類中心點:
center= [[ 0.99602094 1.10688195]
[-1.00828286 -1.05983915]
[ 1.07892315 -0.94286826]]
array([1, 2, 0], dtype=int64)


效果評估:

score_funcs = [
    metrics.adjusted_rand_score,#ARI
    metrics.v_measure_score,#均一性和完整性的加權平均
    metrics.adjusted_mutual_info_score,#AMI
    metrics.mutual_info_score,#互資訊
]
迭代對每個評估函式進行評估操作
for score_func in score_funcs:
    t0 = time.time()
    km_scores = score_func(Y,km_y_hat)
    print("K-Means演算法:%s評估函式計算結果值:%.5f;計算消耗時間:%0.3fs" % 
      (score_func.__name__,km_scores, time.time() - t0))
    
    t0 = time.time()
    mbkm_scores = score_func(Y,mbkm_y_hat)
    print("Mini Batch K-Means演算法:%s評估函式計算結果值:%.5f;計算消耗時間:%0.3fs\n" % 
      (score_func.__name__,mbkm_scores, time.time() - t0))
3153092-65516d466304d648.png

08 聚類演算法 - 聚類演算法的衡量指標

相關文章