二十一、案例实战:聚类实践

 本节内容:

  • 多种聚类算法概述
  • 聚类案例实战

1、多种聚类算法概述

 

 

KMeans+DBSCAN算法

KMeans算法是一种聚类算法,而聚类问题在机器学习中主要是是一类无监督问题:简单理解是手中无标签,我们要去分出标签。这类问题的难点是:如何去评估,也就是怎么来评估我们算法的好坏。如何调参?也就是到底有多少个聚类中心点。
KMeans最大的问题就是一个先验知识,这个先验知识是让我们知道如何对Kmeans要分出来的区域进行一个判断。

DBSCAN同样是一类聚类算法,其算法要点:阈值+核心对象+密度可达(包含直接密度可达)+边界点。

 

2、聚类案例实战

# beer dataset
import pandas as pd
beer = pd.read_csv('data.txt', sep=' ')
beer

 

X = beer[["calories","sodium","alcohol","cost"]]

K-means clustering

from sklearn.cluster import KMeans
# 分别取3个和2个质心的情况
km = KMeans(n_clusters=3).fit(X)
km2 = KMeans(n_clusters=2).fit(X)
# 每个样本点对应的分类

 

km.labels_  #array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 2, 0, 0, 2, 1])

 

beer['cluster'] = km.labels_
beer['cluster2'] = km2.labels_
beer.sort_values('cluster')

 

 

from pandas.tools.plotting import scatter_matrix
%matplotlib inline

cluster_centers = km.cluster_centers_

cluster_centers_2 = km2.cluster_centers_

 

beer.groupby("cluster").mean()

beer.groupby("cluster2").mean()

centers = beer.groupby("cluster").mean().reset_index()  #利用reset_index重置索引

 

 

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 14

 

import numpy as np
colors = np.array(['red', 'green', 'blue', 'yellow'])

 

plt.scatter(beer["calories"], beer["alcohol"],c=colors[beer["cluster"]])

plt.scatter(centers.calories, centers.alcohol, linewidths=3, marker='+', s=300, c='black')

plt.xlabel("Calories")
plt.ylabel("Alcohol")
<matplotlib.text.Text at 0x18a25af4ac8>
 
 
scatter_matrix(beer[["calories","sodium","alcohol","cost"]],s=100, alpha=1, c=colors[beer["cluster"]], figsize=(10,10))
plt.suptitle("With 3 centroids initialized")

scatter_matrix(beer[["calories","sodium","alcohol","cost"]],s=100, alpha=1, c=colors[beer["cluster2"]], figsize=(10,10))
plt.suptitle("With 2 centroids initialized")

 

Scaled data

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_scaled
array([[ 0.38791334,  0.00779468,  0.43380786, -0.45682969],
       [ 0.6250656 ,  0.63136906,  0.62241997, -0.45682969],
       [ 0.82833896,  0.00779468, -3.14982226, -0.10269815],
       [ 1.26876459, -1.23935408,  0.90533814,  1.66795955],
       [ 0.65894449, -0.6157797 ,  0.71672602,  1.95126478],
       [ 0.42179223,  1.25494344,  0.3395018 , -1.5192243 ],
       [ 1.43815906,  1.41083704,  1.1882563 , -0.66930861],
       [ 0.55730781,  1.87851782,  0.43380786, -0.52765599],
       [-1.1366369 , -0.7716733 ,  0.05658363, -0.45682969],
       [-0.66233238, -1.08346049, -0.5092527 , -0.66930861],
       [ 0.25239776,  0.47547547,  0.3395018 , -0.38600338],
       [-1.03500022,  0.00779468, -0.13202848, -0.24435076],
       [ 0.08300329, -0.6157797 , -0.03772242,  0.03895447],
       [ 0.59118671,  0.63136906,  0.43380786,  1.88043848],
       [ 0.55730781, -1.39524768,  0.71672602,  2.0929174 ],
       [-2.18688263,  0.00779468, -1.82953748, -0.81096123],
       [ 0.21851887,  0.63136906,  0.15088969, -0.45682969],
       [ 0.38791334,  1.41083704,  0.62241997, -0.45682969],
       [-2.05136705, -1.39524768, -1.26370115, -0.24435076],
       [-1.20439469, -1.23935408, -0.03772242, -0.17352445]])

 

km = KMeans(n_clusters=3).fit(X_scaled)
beer["scaled_cluster"] = km.labels_
beer.sort_values("scaled_cluster")

 

What are the "characteristics" of each cluster?

beer.groupby("scaled_cluster").mean()

pd.scatter_matrix(X, c=colors[beer.scaled_cluster], alpha=1, figsize=(10,10), s=100)

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A279F8F28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A282989B0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27B5E2E8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27B94F60>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27BE41D0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27C19F28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27C61F60>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27C71C88>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27CF1860>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27D3B7B8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27D7C5C0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27DC6F98>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27E02748>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27E4FEB8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27E8D588>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A27ED47B8>]], dtype=object)

 

 

 

聚类评估:轮廓系数(Silhouette Coefficient )

FAO

  • 计算样本i到同簇其他样本的平均距离ai。ai 越小,说明样本i越应该被聚类到该簇。将ai 称为样本i的簇内不相似度。
  • 计算样本i到其他某簇Cj 的所有样本的平均距离bij,称为样本i与簇Cj 的不相似度。定义为样本i的簇间不相似度:bi =min{bi1, bi2, ..., bik}
  • si接近1,则说明样本i聚类合理
  • si接近-1,则说明样本i更应该分类到另外的簇
  • 若si 近似为0,则说明样本i在两个簇的边界上。
from sklearn import metrics
score_scaled = metrics.silhouette_score(X,beer.scaled_cluster)
score = metrics.silhouette_score(X,beer.cluster)
print(score_scaled, score)#0.179780680894 0.673177504646

 

scores = []
for k in range(2,20):
    labels = KMeans(n_clusters=k).fit(X).labels_
    score = metrics.silhouette_score(X, labels)
    scores.append(score)

scores

[0.69176560340794857,
0.67317750464557957,
0.58570407211277953,
0.42254873351720201,
0.4559182167013377,
0.43776116697963124,
0.38946337473125997,
0.39746405172426014,
0.33061511213823314,
0.34131096180393328,
0.34597752371272478,
0.31221439248428434,
0.30707782144770296,
0.31834561839139497,
0.28495140011748982,
0.23498077333071996,
0.15880910174962809,
0.084230513801511767]

plt.plot(list(range(2,20)), scores)
plt.xlabel("Number of Clusters Initialized")
plt.ylabel("Sihouette Score")

 

DBSCAN clustering

from sklearn.cluster import DBSCAN
db = DBSCAN(eps=10, min_samples=2).fit(X)
labels = db.labels_
beer['cluster_db'] = labels
beer.sort_values('cluster_db')

 

beer.groupby('cluster_db').mean()

 

pd.scatter_matrix(X, c=colors[beer.cluster_db], figsize=(10,10), s=100)

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A278A3940>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A284C56D8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A28501CF8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A28550080>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A2856C588>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A285D1F60>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A286211D0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A2865AF98>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A286AABA8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A286E7278>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A2872E390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A287396A0>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000018A287BC358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A28B356A0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A28B71240>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000018A28BBC470>]], dtype=object)

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2018-12-18 21:46  大头swag  阅读(644)  评论(0)    收藏  举报