Lihat skor siluet
Formula untuk i th data titik
(b(i) - a(i)) / max(a(i),b(i))
di mana b (i) -> ketidaksamaan dari klaster tetangga terdekat
a (i) -> ketidaksamaan antara titik dalam cluster
Ini memberikan skor antara -1 dan +1.
Penafsiran
+1 artinya sangat pas
-1 berarti kesalahan klasifikasi [seharusnya milik cluster yang berbeda]
Setelah menghitung skor siluet untuk setiap titik data, Anda dapat melakukan panggilan pada pilihan untuk jumlah cluster.
Contoh Kode
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_samples, silhouette_score
X, y = make_blobs(n_samples=500,
n_features=2,
centers=4,
cluster_std=1,
center_box=(-10.0, 10.0),
shuffle=True,
random_state=1) # For reproducibility
range_n_clusters = [2, 3, 4, 5, 6]
for n_clusters in range_n_clusters:
# Initialize the clusterer with n_clusters value and a random generator
# seed of 10 for reproducibility.
clusterer = KMeans(n_clusters=n_clusters, random_state=10)
cluster_labels = clusterer.fit_predict(X)
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(X, cluster_labels)
print("For n_clusters =", n_clusters,
"The average silhouette_score is :", silhouette_avg)
# Compute the silhouette scores for each sample
sample_silhouette_values = silhouette_samples(X, cluster_labels)