散点图通过颜色区分聚类 plotly python

4

我正在使用plotly(以便在悬停时获取点信息)来可视化我的聚类散点图。我在为使用KMeans生成的不同聚类分配颜色方面遇到了麻烦。当我在matplotlib.pyplot(作为plt)中绘制时,我使用以下代码:

plt.scatter(restult[:,0], result[:,1], c=cluster_labels

cluster_labels 是指:

n_clusters = 3
km = KMeans(n_clusters).fit(result)
labels = km.labels_

它完全正常运行,但我需要吸尘器信息。

这是目前使用plotly的情况:

trace = go.Scatter(
    x = result[:,0],
    y = result[:,1],
    mode = 'markers',
    text = index, # I want to see the index of each point
)
data = [trace]

# Plot and embed in ipython notebook!
py.iplot(data, filename='basic-scatter')

我感谢您的帮助!

2个回答

9
  • 让我们使用鸢尾花数据集
  • kmeans的标签被用作颜色(marker=dict(color=kmeans.labels_)),就像在matplotlib中一样

from sklearn import datasets
from sklearn import cluster
import plotly
plotly.offline.init_notebook_mode()

iris = datasets.load_iris()
kmeans = cluster.KMeans(n_clusters=3, 
                        random_state=42).fit(iris.data[:,0:2])
data = [plotly.graph_objs.Scatter(x=iris.data[:,0], 
                                  y=iris.data[:,1], 
                                  mode='markers',     
                                  marker=dict(color=kmeans.labels_)
                                  )
       ]
plotly.offline.iplot(data)

enter image description here


-1

进一步扩展Maximilian的方法 - 如果您使用的是sklearn版本>=0.17,则需要重新调整数组大小,因为在0.17中传递1d数组已被弃用。

这里有一个带有重新调整大小的示例:

x = df[df.columns[1]]
x = x.values.reshape(-1,1)
y = df[df.columns[2]]
y = y.values.reshape(-1,1)

kmeans = cluster.KMeans(n_clusters = 3, random_state = 0).fit(x, y)


trace1 = go.Scatter(
x = df[df.columns[1]],
y = df[df.columns[2]],
mode = 'markers',
marker=dict(color=kmeans.labels_,
            size = 7.5,
            line = dict(width=2)
           ),
text =  df.index,
name='Actual'
)

那段代码什么也没做,没有任何结果输出。 - Miguel 2488
这只是伪代码。将x和y替换为您自己的数据,并从上面的答案中导入库... - Joel Alcedo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接