python - 使用单个集群的外部坐标可视化集群
问题描述
我想可视化我的集群。
通过使用此代码:
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()
将索引返回到短数组中。返回的点形成一个多边形。需要在最后复制第一个点以绘制包含闭合多边形的线段。
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()
推荐阅读
- c++ - 如何在 C++ 中清除 unordered_map 的 unordered_map?
- php - PHP中唯一的4个字符代码
- php - 在php中使用输入和输出参数调用SQL Server存储过程
- javafx - JavaFX - 带有 StackPane 的 ScrollPane - 当 viewportBounds 更改时,StackPane 的子项未布局
- python - Python Selenium,相同的表
- c# - 我应该怎么做才能使部署到 sccm 的应用程序使用 c# 显示在客户的软件中心?
- r - 具有正值和负值的 R ggplot 气泡图图例
- python - Python中的单页PDF生成
- assembly - MIPS 程序集 - 打开文件失败
- asp.net-mvc - 路由问题:被路由到错误的视图