特徵對叢集品質的影響
探索個別特徵如何影響 KMeans 模型的分群效能。資料集 X 根據 3 個特徵進行顧客區隔:收入、家中小孩數量,以及家中青少年數量。
silhouette_score 函式與變數 column_names 已為你預先載入。
本練習屬於課程
Python 的 Explainable AI
練習說明
- 求出原始的 silhouette 分數(
original_score)。 - 在 for 迴圈中,逐一移除特徵,並將結果存入
X_reduced。 - 計算新的 silhouette 分數(
new_score)。 - 計算該特徵的
impact。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
kmeans = KMeans(n_clusters=5, random_state=10, n_init=10).fit(X)
# Derive the original silhouette score
original_score = ____
for i in range(X.shape[1]):
# Remove feature at index i
X_reduced = ____
kmeans.fit(X_reduced)
# Compute the new silhouette score
new_score = ____
# Compute the feature's impact
impact = ____
print(f'Feature {column_names[i]}: Impact = {impact}')