我只是在蟒蛇新手

我已经在Internet上搜索了代码以使用scikit执行K-means,我尝试修改了代码以使图3d可视化并为每个群集(3个群集)着色,但是结果是所有具有相同颜色的群集,代码和可视化如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans
from collections import Counter
from mpl_toolkits.mplot3d import Axes3D
from pylab import *

X = np.array([[1, 2, 5],
              [5, 8, 2],
              [1.5, 1.8, 6],
              [8, 8, 9],
              [1, 0.6, 10],
              [2.5, 3.8, 6],
              [2.5, 5.8, 9],
              [5, 8, 3],
              [4, 0.6, 7],
              [2.5, 1.8, 4.6],
              [6.5, 1.8, 12],
              [7, 8, 9],
              [2, 0.6, 7],
              [5.5, 1.8, 4],
              [4.8, 6.9, 6],
              [4.9, 9.8, 2],
              [9, 11, 12]])


cluster_num = 3

kmeans = KMeans(n_clusters=cluster_num)
kmeans.fit(X)

centroids = kmeans.cluster_centers_
labels = kmeans.labels_

print "centroids : "
print centroids
print "labels : "
print labels

colors = ["g.","r.","c.","y."]

color = np.random.rand(cluster_num)

c = Counter(labels)


fig = figure()
ax = fig.gca(projection='3d')


for i in range(len(X)):
    print("coordinate:",X[i], "label:", labels[i])
    print "i : ",i
    print "color[labels[i]] : ",color[labels[i]]
    ax.scatter(X[i][0], X[i][1], X[i][2], c=color[labels[i]])


for cluster_number in range(cluster_num):
  print("Cluster {} contains {} samples".format(cluster_number, c[cluster_number]))

ax.scatter(centroids[:, 0],centroids[:, 1], centroids[:, 2], marker = "x", s=150, linewidths = 5, zorder = 100)

plt.show()


python - 如何以每个群集独特的颜色可视化输出群集-LMLPHP
如何使每个群集具有自己的颜色的可视化?谢谢

最佳答案

现在,color = np.random.rand(cluster_num)正在生成三个随机数,在ax.scatter(X[i][0], X[i][1], X[i][2], c=color[labels[i]])中,您尝试将这些随机数分配为颜色。

相反,您可以更改color = ["g", "r", "b"],以使第一个群集为绿色,第二个群集为红色,第三个群集为蓝色。

对于集群中心,传递相同的参数:

ax.scatter(centroids[:, 0],centroids[:, 1], centroids[:, 2], marker = "x", s=150, linewidths = 5, zorder = 100, c=color)


python - 如何以每个群集独特的颜色可视化输出群集-LMLPHP

关于python - 如何以每个群集独特的颜色可视化输出群集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36438857/

10-12 16:37