One cluster (K = 1) is a possible valid, best fit, for different values of K in K-means clustering. "silhouette_score" in scikit-learn (v 0.23.1) does not seem to work with one cluster and gives an unexpected error.
Here is the code to reproduce:
import numpy as np
from sklearn.metrics import silhouette_samples, silhouette_score
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
labels = kmeans.predict(X)
print(labels)
print(silhouette_score(X, labels)) # 2 clusters works
kmeans = KMeans(n_clusters=1, random_state=0).fit(X)
labels = kmeans.predict(X)
print(labels)
print(silhouette_score(X, labels)) # 1 cluster gives ValueError: Number of labels is 1. Valid values are 2 to n_samples - 1 (inclusive)
The correct value of silhouette score for 1 cluster should be zero according to this.
Am I doing something wrong here?