title: Why is the k-means clustering scatter plot showing different results?
body: <p>I have two datasets(data.csv, label.csv).The &quot;label.csv&quot; dataset contains the gene names of the samples on &quot;data.csv&quot; dataset. I want to run k-means clustering algorithm in those datasets and make a scatter plot. But I am having difficulties in it. Below I have given the code that I performed for getting the clustering done.</p>
  <pre><code>import pandas as pd
  from sklearn.cluster import KMeans
  
  X = pd.read_csv(&quot;data.csv&quot;)
  Y = pd.read_csv(&quot;labels.csv&quot;)
  
  #reduce the dimension of X
  import time
  from sklearn.manifold import TSNE
  import matplotlib.pyplot as plt
  from sklearn.preprocessing import LabelEncoder
  
  X = X.dropna()
  # drop the first column which only contains strings
  X = X.drop(X.columns[X.columns.str.contains('unnamed', case=False)], axis=1)
  
  # label encode the multiple class string into integer values
  Y = Y.drop(Y.columns[0], axis=1)
  le = LabelEncoder()
  le.fit(Y)
  class_names = list(le.classes_)
  
  Y = Y.apply(LabelEncoder().fit_transform)
  Y_data = Y.values.flatten()
  
  # use TSNE to visualize the high dimension data in 2D
  t0 = time.time()
  tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300, random_state=100)
  tsne_results = tsne.fit_transform(X)
  t1 = time.time()
  print(&quot;TSNE took at %.2f seconds&quot; % (t1 - t0))
  
  # visualize TSNE
  x_axis = tsne_results[:,0]
  y_axis = tsne_results[:,1]
  
  plt.scatter(x_axis, y_axis, c=Y_data, cmap=plt.cm.get_cmap(&quot;jet&quot;, 100))
  
  plt.colorbar(ticks=range(10))
  plt.clim(-0.5, 9.5)
  plt.title(&quot;TSNE Visualization&quot;)
  plt.show()
  </code></pre>
  <p>The code above gives a scatter plot(given below) of the 5 different classes in my datasets. the classes are colored differently from 0 to 4 (5 classes).</p>
  <p><a href="https://i.stack.imgur.com/VqdbM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VqdbM.png" alt="enter image description here" /></a></p>
  <p>But when I apply K-means clustering code(given below) it shows 5 clusters but the classes colors are not from 0 0 to 4 instead it ranges from 0 to 9. The scatter plot image is given below the k means cluster code.</p>
  <pre><code>import matplotlib.pyplot as plt
  from sklearn.cluster import KMeans
  
  model=KMeans(n_clusters=5)
  model.fit(tsne_results)
  label=model.predict(tsne_results)
  
  #centroid calculation
  xs =tsne_results[:,0]
  ys =tsne_results[:,1]
  plt.scatter(xs,ys,c=Y_data,alpha=0.5)
  
  centroids = model.cluster_centers_
  centroids_x = centroids[:,0]
  centroids_y = centroids[:,1]
  
  plt.scatter(centroids_x, centroids_y, marker='D', s=50)
  plt.colorbar(ticks=range(10))
  plt.clim(-0.5, 9.5)
  plt.show()
  </code></pre>
  <p><a href="https://i.stack.imgur.com/HRCq2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HRCq2.png" alt="enter image description here" /></a></p>
  <p>Now, in which part of the code do I need to bring change in order to get a scatter plot like the first one showing the colored classes from 0 to 4 for 5 clusters.</p>