【问题标题】:Visualisation of clusters using outer coordinates of the individual cluster使用单个集群的外部坐标可视化集群
【发布时间】:2021-01-19 12:55:53
【问题描述】:

我想可视化我的集群。

通过使用此代码:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

X = np.array([[28, 7], [36, 5], [32, 2], [56, 8], [47, 5], [50,100], [100,100], [26,59], [19,71],
              [75, 9], [34, 4], [56, 9], [28, 1], [33, 6]])
col = ['blue', 'green', 'c', 'm', 'y', 'k', "violet", "indigo"]
ncluster = 2
kmeans = KMeans(n_clusters=ncluster, max_iter=500).fit(X)
y = kmeans.labels_
centroids = kmeans.cluster_centers_
clusters_centroids = dict()
clusters_radii = dict()
for cluster in range(ncluster):
    clusters_centroids[cluster] = list(
        zip(centroids[:, 0], centroids[:, 1]))[cluster]
    clusters_radii[cluster] = max([np.linalg.norm(np.subtract(
        i, clusters_centroids[cluster])) for i in zip(X[y == cluster, 0], X[y == cluster, 1])])

fig, ax = plt.subplots(1, figsize=(7, 5))

def drawclusters():
    for i in range(ncluster):
        plt.scatter(X[y == i, 0], X[y == i, 1], s=100,
                    c=col[i], label=f'Cluster {i + 1}')
        art = mpatches.Circle(
            clusters_centroids[i], clusters_radii[i], edgecolor=col[i], fill=False)
        ax.add_patch(art)
    plt.scatter(centroids[:, 0], centroids[:, 1], s=200,
                c='red', label='Centroids', marker='x')


drawclusters()
plt.legend()
plt.tight_layout()
plt.show()

我得到了圈子:

但我想使用与此类似的点进行可视化忽略数据部分,我只需要可视化部分(我需要形状):

我需要 python 中的代码。 R中有一个函数fviz_cluster

【问题讨论】:

    标签: python matplotlib scikit-learn k-means graph-visualization


    【解决方案1】:

    您可以使用scipy.spatial.ConvexHull() 创建每个集群的凸包。请注意,X[y == i] 需要转换为新数组,因为ConvexHull() 将索引返回到短数组中。返回的点形成一个多边形。需要在最后复制第一个点以绘制包含闭合多边形的线段。

    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn.cluster import KMeans
    from scipy.spatial import ConvexHull
    
    def drawclusters(ax):
        for i in range(ncluster):
            points = X[y == i]
            ax.scatter(points[:, 0], points[:, 1], s=100, c=col[i], label=f'Cluster {i + 1}')
            hull = ConvexHull(points)
            vert = np.append(hull.vertices, hull.vertices[0])  # close the polygon by appending the first point at the end
            ax.plot(points[vert, 0], points[vert, 1], '--', c=col[i])
            ax.fill(points[vert, 0], points[vert, 1], c=col[i], alpha=0.2)
        ax.scatter(centroids[:, 0], centroids[:, 1], s=200, c='red', label='Centroids', marker='x')
    
    X = np.array([[28, 7], [36, 5], [32, 2], [56, 8], [47, 5], [50, 100], [100, 100], [26, 59], [19, 71],
                  [75, 9], [34, 4], [56, 9], [28, 1], [33, 6]])
    col = ['blue', 'green']
    ncluster = 2
    kmeans = KMeans(n_clusters=ncluster, max_iter=500).fit(X)
    y = kmeans.labels_
    centroids = kmeans.cluster_centers_
    fig, ax = plt.subplots(1, figsize=(7, 5))
    drawclusters(ax)
    ax.legend()
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 2018-07-11
      • 2020-03-30
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2016-07-26
      • 2019-05-13
      • 2018-09-07
      相关资源
      最近更新 更多