machine-learning - 亲和性传播首选项初始化

标签 machine-learning scikit-learn cluster-analysis unsupervised-learning

我需要在事先不知道聚类数量的情况下执行聚类。簇的数量可以是 1 到 5,因为我可能会发现所有样本都属于同一实例或属于有限数量的组的情况。 我认为亲和性传播可能是我的选择,因为我可以通过设置首选项参数来控制集群的数量。 但是,如果我人工生成了一个集群,并且我将偏好设置为节点之间的最小欧几里德距离(以最小化集群数量),那么我会因为集群而变得很糟糕。

"""
=================================================
Demo of affinity propagation clustering algorithm
=================================================

Reference:
Brendan J. Frey and Delbert Dueck, "Clustering by Passing Messages
Between Data Points", Science Feb. 2007

"""
print(__doc__)
import numpy as np
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from scipy.spatial.distance import pdist

##############################################################################
# Generate sample data
centers = [[0,0],[1,1]]
X, labels_true = make_blobs(n_samples=300, centers=centers, cluster_std=0.5,
                            random_state=0)
init = np.min(pdist(X))

##############################################################################
# Compute Affinity Propagation
af = AffinityPropagation(preference=init).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()

enter image description here

我使用亲和性传播的方法有什么缺陷吗?相反,亲和性传播是否不适合这项任务,那么我应该使用其他方法吗?

最佳答案

不,没有任何缺陷。 AP 不使用距离,但要求您指定相似度。我不太了解 scikit 实现,但根据我读到的内容,它默认使用平方欧几里得距离来计算相似度矩阵。如果将输入首选项设置为最小欧几里德距离,您将获得正值,而所有相似度均为负值。因此,这通常会产生与样本一样多的聚类(注意:输入偏好越高,聚类越多)。我宁愿建议将输入首选项设置为最小负平方距离,即数据集中最大距离的平方的-1倍。这将为您提供数量少得多的集群,但不一定是一个集群。我不知道 preferenceRange() 函数是否也存在于 scikit 实现中。 AP 主页上有 Matlab 代码,它也在我维护的 R 包“apcluster”中实现。该函数允许确定输入偏好参数的有意义的界限。我希望这会有所帮助。

关于machine-learning - 亲和性传播首选项初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187354/

相关文章:

scikit-learn - 如何使用 scikit-learn 从线性判别分析中获取特征向量

python - 是否可以在多输入神经网络中使用 sklearn 中的 StratifiedKFold?

matlab - 聚类和贝叶斯分类器 Matlab

matlab - 在 Matlab 中查找具有高互相关矩阵的组

matlab - 有没有基于 GPU 的 Matlab 深度学习工具箱?

machine-learning - 一次可以向 Caffe 传递多少张图像?

python - sklearn的KNeighborsClassifier中如何根据最高精度选择K

python - 在轨迹上运行 DBSCAN

python - 检测灰度图像中是否存在黑色区域

python - AWS Sagemaker SKlearn 入口点允许多个脚本