title: Calculate distance between data points and centroid (kmeans) using sklearn in python
body: <p>I am trying to do clustering using kmeans by sklearn. I need to compute the distance between each data point and its centroid. Can anyone help me here.</p>
  
  <p>Thanks in advance</p>
  
  <pre><code>import re
  import sys
  import os
  import sklearn.datasets
  from sklearn.cluster import KMeans
  dataFilename = sys.argv[1]
  X, y = sklearn.datasets.load_svmlight_file(dataFilename)
  kmeans = KMeans(n_clusters=3)
  kmeans.fit(X)
  centroids = kmeans.cluster_centers_
  labels = kmeans.predict(X)
  j=0
  intervals = []
  for i in range(0,max(labels)+1):
      intervals.append([])
  print(intervals)
  for i in y:
      intervals[labels[j]].append(int(y[j]))
      j += 1
  print(intervals)
  </code></pre>