Issue
I'm using sklearn to do a kmeans cluster based on some retail data.
We're using this cluster behind the scenes to segment customers (e.g., Blue customers are great, green customers have such-and-such a need, etc.).
The datapoints shown are a different color based on which of 4 clusters a customer has been put into. But I can't find a way to directly infer which color is which segment number (or how to force certain segment numbers to be a certain color).
c=y
in the scatter is where it's using the value of y (i.e., the predicted segment for an observation) to pick the color. There are 4 segments. I just don't know which of those 4 gets mapped to which color!
Can someone please advise on how I can add a legend, or force the color myself?
kmeans=kmeans.fit(X)
y=kmeans.predict(X)
fig = plt.figure()
ax = Axes3D(fig)
ax.view_init(30)
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y)
ax.set_xlabel(vx)
ax.set_ylabel(vy)
ax.set_zlabel(vz)
Solution
You need to return a handle from scatter and plot colorbar,
cm = ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y)
plt.colorbar(cm)
As a minimal example
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X = np.random.randn(100,3)
y = np.random.randn(100)
fig = plt.figure()
ax = Axes3D(fig)
ax.view_init(30)
cm = ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y)
plt.colorbar(cm)
plt.show()
Answered By - Ed Smith
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.