Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 散点图通过颜色图分离簇_Python 2.7_Cluster Computing_Plotly_Scatter Plot - Fatal编程技术网

Python 2.7 散点图通过颜色图分离簇

Python 2.7 散点图通过颜色图分离簇,python-2.7,cluster-computing,plotly,scatter-plot,Python 2.7,Cluster Computing,Plotly,Scatter Plot,我正在使用plotly(以便在胡佛过来时能够获得点信息)来可视化我的聚集散点图。我在使用KMeans生成的簇中分配不同的颜色时遇到了问题。在matplotlib.pyplot(作为plt)中打印时,我使用以下代码: plt.scatter(restult[:,0], result[:,1], c=cluster_labels 群集标签为: n_clusters = 3 km = KMeans(n_clusters).fit(result) labels = km.labels_ 它工作得很好

我正在使用plotly(以便在胡佛过来时能够获得点信息)来可视化我的聚集散点图。我在使用KMeans生成的簇中分配不同的颜色时遇到了问题。在matplotlib.pyplot(作为plt)中打印时,我使用以下代码:

plt.scatter(restult[:,0], result[:,1], c=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')
我感谢你的帮助

  • 让我们使用iris数据集
  • kmeans中的标签用作颜色(
    marker=dict(color=kmeans.labels)
    ),就像在matplotlib中一样



仅扩展Maxmimilian的方法-如果您使用的是sklearn version>=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'
)

那代码什么都没做,没有任何阴谋,只是伪代码。用您自己的数据替换x和y,并从上面的答案导入库。。。
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'
)