use*_*653 2 visualization cluster-analysis matplotlib
我对 Feature1、Feature2 和 Feature3 3 个特征进行了聚类,并提出了 2 个聚类。我正在尝试使用 matplotlib 可视化 3D 集群。
在下表中,有执行聚类的三个特征。簇数为 2。
Feature1 Feature2 Feature3 ClusterIndex
0 1.349656e-09 1.000000 1.090542e-09 0
1 1.029752e-07 1.000000 6.040669e-08 0
2 2.311729e-07 1.000000 1.568289e-11 0
3 1.455860e-08 6.05e-08 1.000000 1
4 3.095807e-07 2.07e-07 1.000000 1
Run Code Online (Sandbox Code Playgroud)
试过这个代码:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.array(df['Feature1'])
y = np.array(df['Feature2'])
z = np.array(df['Feature3'])
ax.scatter(x,y,z, marker=colormap[kmeans.labels_], s=40)
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误"ValueError: could not convert string to float: red"。因此,标记部分是我得到错误的地方。
通过在散点图中绘制点并使用集群标签进行区分,集群的 2D 可视化非常简单。
只是想知道有没有办法对集群进行 3D 可视化。
任何建议将不胜感激!
原则上,问题中的代码应该可以工作。然而,目前尚不清楚marker=colormap[kmeans.labels_]会做什么以及为什么需要它。
3D 散点图的工作原理与它的 2D 版本完全相同。
标记参数需要一个标记字符串,例如"s"或"o"来确定标记形状。
可以使用c参数设置颜色。您可以提供单一颜色或数组/颜色列表。在下面的示例中,我们只是提供集群索引c并使用颜色图。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import numpy as np
v = np.random.rand(10,4)
v[:,3] = np.random.randint(0,2,size=10)
df = pd.DataFrame(v, columns=['Feature1', 'Feature2','Feature3',"Cluster"])
print (df)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.array(df['Feature1'])
y = np.array(df['Feature2'])
z = np.array(df['Feature3'])
ax.scatter(x,y,z, marker="s", c=df["Cluster"], s=40, cmap="RdBu")
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10054 次 |
| 最近记录: |