对稀疏共生矩阵进行聚类

rei*_*ore 4 python cluster-analysis matrix sparse-matrix

我有两个 N x N 共现矩阵(484x484 和 1060x1060)需要分析。矩阵沿对角线对称,并包含许多零值。非零值是整数。

我想将非零的位置组合在一起。换句话说,我想做的是这个链接上的算法。When order by cluster is selected, the matrix gets re-arranged in rows and columns to group the non-zero values together.

由于我将 Python 用于此任务,因此我查看了SciPy 稀疏线性代数库,但找不到我要查找的内容。

任何帮助深表感谢。提前致谢。

小智 6

如果你有一个dist对象之间成对距离的矩阵,那么你可以通过在这个矩阵上应用聚类算法来找到重新排列矩阵的顺序(http://scikit-learn.org/stable/modules/clustering.html) . 例如它可能是这样的:

from sklearn import cluster
import numpy as np
model = cluster.AgglomerativeClustering(n_clusters=20,affinity="precomputed").fit(dist)
new_order = np.argsort(model.labels_)
ordered_dist = dist[new_order] # can be your original matrix instead of dist[]
ordered_dist = ordered_dist[:,new_order]
Run Code Online (Sandbox Code Playgroud)

顺序由变量 给出,该变量model.labels_具有每个样本所属的集群编号。一些观察:

  1. 您必须找到一种接受距离矩阵作为输入的聚类算法。AgglomerativeClustering就是这样一种算法(注意affinity="precomputed"告诉它我们正在使用预先计算的距离的选项)。
  2. 您所拥有的似乎是成对相似矩阵,在这种情况下,您需要将其转换为距离矩阵(例如dist=1 - data/data.max()
  3. 在我假设有 20 个集群的示例中,您可能需要稍微处理一下这个变量。或者,您可能会尝试找到数据的最佳一维表示(使用例如MDS)来描述样本的最佳排序。