美文网首页
Kmeans-DBScan

Kmeans-DBScan

作者: ForgetThatNight | 来源:发表于2018-07-06 21:34 被阅读21次
    # 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')
    
    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()
    
    %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")
    
    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)
    

    聚类评估:轮廓系数(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")
    

    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)
    

    相关文章

      网友评论

          本文标题:Kmeans-DBScan

          本文链接:https://www.haomeiwen.com/subject/tdgbuftx.html