data.txt
name calories sodium alcohol cost Budweiser 144 15 4.7 0.43 Schlitz 151 19 4.9 0.43 Lowenbrau 157 15 0.9 0.48 Kronenbourg 170 7 5.2 0.73 Heineken 152 11 5.0 0.77 Old_Milwaukee 145 23 4.6 0.28 Augsberger 175 24 5.5 0.40 Srohs_Bohemian_Style 149 27 4.7 0.42 Miller_Lite 99 10 4.3 0.43 Budweiser_Light 113 8 3.7 0.40 Coors 140 18 4.6 0.44 Coors_Light 102 15 4.1 0.46 Michelob_Light 135 11 4.2 0.50 Becks 150 19 4.7 0.76 Kirin 149 6 5.0 0.79 Pabst_Extra_Light 68 15 2.3 0.38 Hamms 139 19 4.4 0.43 Heilemans_Old_Style 144 24 4.9 0.43 Olympia_Goled_Light 72 6 2.9 0.46 Schlitz_Light 97 7 4.2 0.47
# 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 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')
ModuleNotFoundError: No module named 'pandas.tools'
from pandas.tools.plotting import scatter_matrix
改为:
from pandas.plotting import scatter_matrix
from pandas.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()
%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")
scatter_matrix(beer[["calories","sodium","alcohol","cost"]],s=100, alpha=1, c=colors[beer["cluster"]], figsize=(10,10)) plt.suptitle("With 3 centroids initialized")
Text(0.5, 0.98, '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")
beer.groupby("scaled_cluster").mean()
AttributeError: module 'pandas' has no attribute 'scatter_matrix'
将:
pd.scatter_matrix(X, c=colors[beer.scaled_cluster], alpha=1, figsize=(10,10), s=100)
改为:
pd.plotting.scatter_matrix(X, c=colors[beer.scaled_cluster], alpha=1, figsize=(10,10), s=100)
pd.plotting.scatter_matrix(X, c=colors[beer.scaled_cluster], alpha=1, figsize=(10,10), s=100)
聚类评估:轮廓系数(Silhouette Coefficient )
- 计算样本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")
Text(0, 0.5, '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.plotting.scatter_matrix(X, c=colors[beer.cluster_db], figsize=(10,10), s=100)
array([[<AxesSubplot:xlabel='calories', ylabel='calories'>, <AxesSubplot:xlabel='sodium', ylabel='calories'>, <AxesSubplot:xlabel='alcohol', ylabel='calories'>, <AxesSubplot:xlabel='cost', ylabel='calories'>], [<AxesSubplot:xlabel='calories', ylabel='sodium'>, <AxesSubplot:xlabel='sodium', ylabel='sodium'>, <AxesSubplot:xlabel='alcohol', ylabel='sodium'>, <AxesSubplot:xlabel='cost', ylabel='sodium'>], [<AxesSubplot:xlabel='calories', ylabel='alcohol'>, <AxesSubplot:xlabel='sodium', ylabel='alcohol'>, <AxesSubplot:xlabel='alcohol', ylabel='alcohol'>, <AxesSubplot:xlabel='cost', ylabel='alcohol'>], [<AxesSubplot:xlabel='calories', ylabel='cost'>, <AxesSubplot:xlabel='sodium', ylabel='cost'>, <AxesSubplot:xlabel='alcohol', ylabel='cost'>, <AxesSubplot:xlabel='cost', ylabel='cost'>]], dtype=object)