I'm trying to implement and visualize a K-means algorithm code in Python. I have a dataset I created using make_blobs
, then I fit the data with K-means and visualize the results using matplotlib.pyplot.scatter
.
Here's my code:
Importing and data creation step
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
n_samples = 3000
random_state = 1182
X, y = make_blobs(n_samples=n_samples, random_state=random_state)
# X.shape = (3000, 2)
# y.shape = (3000,) -> y's values range from 0 to 2.
Scatter plot of original data
plt.scatter(X[:, 0], X[:, 1])
plt.title("Original Dataset Scatter Plot")
plt.xlabel("X[:, 0]")
plt.ylabel("X[:, 1]")
plt.show()
K-Means training and visualization
kmeans_model = KMeans(n_clusters=3, random_state=1)
kmeans_model.fit(X)
colors = { 0: 'r',
1: 'b',
2: 'g'}
label_color = [colors[l] for l in y]
plt.scatter(X[:, 0], kmeans_model.labels_, c=label_color)
plt.title("K-Means Scatter Plot")
plt.xlabel("X[:, 0]")
plt.ylabel("Labels")
plt.show()
My question is: when I use plt.scatter
with X[:, 1]
instead of X[:, 0]
, as I did in the given code, I get a different plot albeit with the same clusters as such:
Would this still be considered a correct implementation and usage of K-means and scatter plots? If so, is there a particular reason that one should choose certain x
values over others?