【问题标题】:Input format for AffinityPropagation clusteringAffinityPropagation 聚类的输入格式
【发布时间】:2014-05-17 09:44:40
【问题描述】:

我使用scipy.cluster.hierarchy.linkage 方法使用预先计算的亲和矩阵:

这是生成上三角矩阵的代码:

distances = np.zeros((len(reprs), len(reprs))) * -1
for i, j in it.combinations(range(len(reprs)), 2):
    distances[i][j] = (reprs[i] - reprs[j])**2

我也可以简洁地表示:

distances = distances[np.triu_indices(len(reprs), 1)]

现在我想试试sklearn.cluster.AffinityPropagation,但我不知道如何发送亲和力矩阵:

def affinity_cluster(distances):
    ap = sklearn.cluster.AffinityPropagation(preference="precomputed")
    d = ap.fit_predict(???)

来自documentation:

适合(X) 从负欧几里德距离创建亲和矩阵,然后应用亲和传播聚类。 参数:

X: array [n_samples, n_features] or [n_samples, n_samples] :
    Data matrix or, if affinity is precomputed, matrix of similarities / affinities.

fit_predict(X, y=None) 对 X 执行聚类并返回聚类标签。 参数:

X : ndarray, shape (n_samples, n_features)
    Input data.

Returns : 

y : ndarray, shape (n_samples,)
    cluster labels

所以,他们期待两个元素的元组,但我有一个 M*N 矩阵或 M*N/2 个元素的向量。

那么,如何将sklearn.cluster.AffinityPropogation 与亲和矩阵一起使用?

【问题讨论】:

    标签: python numpy scipy scikit-learn


    【解决方案1】:

    您需要传递一个方形对称矩阵。 array [n_samples, n_samples] 应该读作array of shape (n_samples, n_samples)。我会在一分钟内修复文档。

    【讨论】:

    • 谢谢!那是一个很棒的图书馆,它对我的​​毕业设计帮助很大。我也在尝试用它来将我的教授从 MATLAB 转换为 Python。当我有时间尝试时会接受答案。
    猜你喜欢
    • 2014-08-07
    • 2018-06-04
    • 2023-03-12
    • 1970-01-01
    • 2016-06-24
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    相关资源
    最近更新 更多