I am trying to compute Silhouette with k-means. However I have the value really close to 0 and the clusters are very clearly separated. Do you know where can be the problem? This is the code:

n_samples, n_features = data.shape
sample_size = 300

print("n_digits: %d, \t n_samples %d, \t n_features %d"
      % (n_digits, n_samples, n_features))
print(79 * '_')
print('% 9s' % 'init'
      '    time  inertia    homo   compl  v-meas     ARI AMI  silhouette')

def bench_k_means(estimator, name, data):
    t0 = time()
    print('% 9s   %.2fs    %i   %.3f   %.3f   %.3f   %.3f   %.3f    %.3f'
          % (name, (time() - t0), estimator.inertia_,
             metrics.homogeneity_score(labels, estimator.labels_),
             metrics.completeness_score(labels, estimator.labels_),
             metrics.v_measure_score(labels, estimator.labels_),
             metrics.adjusted_rand_score(labels, estimator.labels_),
             metrics.adjusted_mutual_info_score(labels,  estimator.labels_),
             metrics.silhouette_score(data, estimator.labels_,

pca = PCA(n_components=n_digits).fit(data)
bench_k_means(KMeans(init=pca.components_, n_clusters=n_digits, n_init=1),

print(79 * '_')

The obtained Silhouette is 0.052 and this Silhouette is the obtained k-means clustering.



  • 2
    $\begingroup$ Your code is unreadable. Please edit your question, use the markdown code feature for writing code. $\endgroup$
    – enterML
    Oct 3 '16 at 9:34
  • 2
    $\begingroup$ How do you know that "clusters are very clearly separated"? Also there seems to be a bug - "data" also contains the labels (in the last column). You should remove the last column with the labels before fitting the model on the data. $\endgroup$
    – stmax
    Oct 3 '16 at 10:47
  • $\begingroup$ First fix the problem mentioned by stmax - remove the labels from your data prior to PCA/clustering: data = data[:,:-1]. Then, show the result, and why you think it's good (and use the formatter to make it readable). This PCA based initialization is supposedly not meaningful - consider sticking to the default initialization. $\endgroup$ Oct 3 '16 at 11:12
  • 1
    $\begingroup$ Hello, I've used the markdown code feature. I have also removed the labels from my data prior to PCA/clustering. I also have added the resulting clustering and the Silhouette. The Silhouette is nearly zero and as you can see in the picture, two clusters are clearly formed. $\endgroup$
    – keira
    Oct 3 '16 at 11:40

Since the points in each cluster are fairly spread out along the direction of the separating line, the average distance within a cluster is probably not that much smaller (as you might hope) than the average distance to points in the other cluster. A small positive value could be plausible. (Maybe look at the distribution of the points along the second principal component. A big spread could be the problem.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.