Reputation: 71
I am using plotly (to be able to get point information when I hoover over) to visualise my clustered scatter plot. I am having trouble with assigning different colours to the clusters I have produced by using KMeans. When plotting this in matplotlib.pyplot (as plt) I use the following code:
plt.scatter(restult[:,0], result[:,1], c=cluster_labels
cluster_labels being:
n_clusters = 3
km = KMeans(n_clusters).fit(result)
labels = km.labels_
And it works totally fine, but I need the hoover info.
This is where I am at so far with 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')
I appreciate the help!
Upvotes: 4
Views: 6123
Reputation: 312
Just to expand on Maxmimilian's method - if you're using sklearn version >=0.17 then you'll need to reshape your array since passing 1d arrays is deprecated in 0.17.
Here's an example with reshaping:
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'
)
Upvotes: -1
Reputation: 31659
marker=dict(color=kmeans.labels_)
), just like in matplotlibfrom 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)
Upvotes: 9