# Silhouette calculation in k-means

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
n_digits=2
labels=data[:,-1]
data=data[:,:-1]
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()
estimator.fit(data)
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.silhouette_score(data, estimator.labels_,
metric='euclidean',
sample_size=sample_size)))

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

print(79 * '_')


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

Thanks,

Laia

• Your code is unreadable. Please edit your question, use the markdown code feature for writing code. – enterML Oct 3 '16 at 9:34
• 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. – stmax Oct 3 '16 at 10:47
• 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. – Has QUIT--Anony-Mousse Oct 3 '16 at 11:12
• 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. – keira Oct 3 '16 at 11:40