python - 如何用每个簇独特的颜色可视化输出簇

标签 python matplotlib scikit-learn

我只是Python新手

我在互联网上搜索使用 scikit 执行 K 均值的代码,我尝试修改代码以可视化图 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()

enter image description here 我怎样才能让每个簇都有自己的颜色可视化?谢谢

最佳答案

现在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)

enter image description here

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

相关文章:

python - 是什么决定了我的 Python 梯度下降算法是否收敛?

python - 设置 `ylabel`的位置

python - scikit-learn 支持/计划使用哪些非负线性模型?

matlab - 在 matlab 中像 matplotlib imshow 一样在 imagesc\imshow 的像素之间进行平滑处理

python - 迭代 RFE 分数 sklearn

machine-learning - 使用 scikit-learn 的自定义功能

Python列表语法解释

python - 为 mechanize.Browser 设置超时

python - 将带括号的字符串转换为嵌套列表

python - 使用 xarray (pcolormesh) 绘制 2D 数据,同时保持纵横比