I have a vector (X2) and I would like to investigate the groups they can form. I have performed the K-means analysis, whith 2 clusters: shape X2 = (19,1)
kmeans = KMeans(n_clusters=2,random_state=123)
kmeans.fit(X2)
label = kmeans.fit_predict(X2)
print(label)
[0 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 0 1 0]
Now I would like to make the scatter plot of these 2 clusters. Could someone help me with the plot.
I'm not sure how I can associate the label with my vector (X2) and what to put in x-axis and y-axis.
I would like to have something like this:
But I don't know how to organize the plot, because the labels are not the x-axis. I now the y-axis (X2), what is missing is the x-axis and how to associate the labels with colors to X2
CodePudding user response:
You can plot your x points as indexes and y points as the real values (X2). And if we come to the point that you want to associate the colors you can use 'c' keyword of matplotlib's scatter method.
As an example:
plt.scatter([i for i in range(len(X2))], X2, c=labels)
color(c) = labels, is going to scatter indexes that has same corresponding label with same color. You can also use c="red" to make every point red.
CodePudding user response:
The plot that you display is a 3-dimensional graphic, the space X (x,y) and the class.
If I understand your question, you are clustering samples which are in one dimension. You don't need a 3-dimensional graph but a 2-dimensional. You can forget the color of the graphic and use the y-axis to represent the class (-1,1).
You can project them on a 2-dimensional space if you want by adding a fake dimension (y=0) and use the color parameter of matplotlib with the class predicted.