# 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

• 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. Oct 3, 2016 at 11:12