使用单个集群的外部坐标可视化集群

Visualisation of clusters using outer coordinates of the individual cluster

我想可视化我的集群。

通过使用此代码:

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.

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

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()