【发布时间】: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