cluster-analysis - 剪影法:分数随着K个数的增加而整体增加
问题描述
与在线教程不同,轮廓图具有全局最大值。我的情节随着 K 数量的增加而整体增加。但我可以找到局部最大值。我应该这样做吗?
我也使用了肘部方法。但是,曲线是平坦的,很难确定肘部。
解决方案
帮助我弄清楚 K 的一件事是运行 Affinity Propagation。这将为您确定最佳 K,因此您不必猜测。请参见下面的示例。
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets import make_blobs
# #############################################################################
# Generate sample data
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5,
random_state=0)
# #############################################################################
# Compute Affinity Propagation
af = AffinityPropagation(preference=-50).fit(X)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
n_clusters_ = len(cluster_centers_indices)
print('Estimated number of clusters: %d' % n_clusters_)
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
print("Adjusted Rand Index: %0.3f"
% metrics.adjusted_rand_score(labels_true, labels))
print("Adjusted Mutual Information: %0.3f"
% metrics.adjusted_mutual_info_score(labels_true, labels))
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels, metric='sqeuclidean'))
# #############################################################################
# Plot result
import matplotlib.pyplot as plt
from itertools import cycle
plt.close('all')
plt.figure(1)
plt.clf()
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()
# Result:
Estimated number of clusters: 3
Homogeneity: 0.872
Completeness: 0.872
V-measure: 0.872
Adjusted Rand Index: 0.912
Adjusted Mutual Information: 0.871
Silhouette Coefficient: 0.753
添加 Affinity Propagation 库、将数据输入其中、获得最佳 K 并将该数字添加到 K-Means 算法中应该是一个简单的练习。或者,只需使用 Affinity Propagation。这也是一种选择。
推荐阅读
- intellij-idea - 用于转储整个堆栈/调用跟踪以进行比较的资源
- phpspreadsheet - PHPSpreadsheet 无法保存从模板加载的折线图(被“另一个用户”锁定)
- r - Ewma 的时间序列回报
- flutter - Flutter 与其他应用程序共享链接,并带有单独的描述
- cordova - 离子cordova插件cordova-plugin-googleplus错误10
- python - Pygame Zero 中 Actor 函数的“pos”不起作用
- c# - DDD 逻辑中的批量操作
- java - 扫描仪输入和数组重复验证导致无限循环
- reactjs - 当我从外部更改 Picker 的值时,RNUI Picker 的列表没有正确更新
- powershell - 从用于使用 PowerShell 登录 Azure-AD 的交互式凭据获取 Graph 访问令牌