聚类稀疏共现矩阵
Clustering a sparse co-occurrence matrix
我有两个 N x N 共现矩阵(484x484 和 1060x1060)需要分析。矩阵沿对角线对称并且包含许多零值。非零值是整数。
我想将非零位置组合在一起。也就是说,我要做的就是算法on this link。 When order by cluster is selected, the matrix gets re-arranged in rows and columns to group the non-zero values together.
因为我正在使用 Python 来完成这项任务,所以我查看了 SciPy Sparse Linear Algebra 库,但找不到我要找的东西。
非常感谢任何帮助。提前致谢。
如果您的矩阵 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]
顺序由变量model.labels_
给出,其中有每个样本所属的簇的编号。几点观察:
- 你必须找到一个接受距离矩阵作为输入的聚类算法。 AgglomerativeClustering 就是这样一种算法(注意
affinity="precomputed"
选项告诉它我们正在使用预先计算的距离)。
- 你所拥有的似乎是一个成对相似矩阵,在这种情况下你需要将其转换为距离矩阵(例如
dist=1 - data/data.max()
)
- 在我假设有 20 个集群的示例中,您可能需要稍微玩一下这个变量。或者,您可以尝试找到数据的最佳一维表示(例如使用 MDS)来描述样本的最佳排序。
因为您的数据是稀疏的,将其视为图形,而不是矩阵。
然后尝试各种图形聚类方法。例如,小团体对此类数据很感兴趣。
请注意,并非所有东西都可能聚集。
我有两个 N x N 共现矩阵(484x484 和 1060x1060)需要分析。矩阵沿对角线对称并且包含许多零值。非零值是整数。
我想将非零位置组合在一起。也就是说,我要做的就是算法on this link。 When order by cluster is selected, the matrix gets re-arranged in rows and columns to group the non-zero values together.
因为我正在使用 Python 来完成这项任务,所以我查看了 SciPy Sparse Linear Algebra 库,但找不到我要找的东西。
非常感谢任何帮助。提前致谢。
如果您的矩阵 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]
顺序由变量model.labels_
给出,其中有每个样本所属的簇的编号。几点观察:
- 你必须找到一个接受距离矩阵作为输入的聚类算法。 AgglomerativeClustering 就是这样一种算法(注意
affinity="precomputed"
选项告诉它我们正在使用预先计算的距离)。 - 你所拥有的似乎是一个成对相似矩阵,在这种情况下你需要将其转换为距离矩阵(例如
dist=1 - data/data.max()
) - 在我假设有 20 个集群的示例中,您可能需要稍微玩一下这个变量。或者,您可以尝试找到数据的最佳一维表示(例如使用 MDS)来描述样本的最佳排序。
因为您的数据是稀疏的,将其视为图形,而不是矩阵。
然后尝试各种图形聚类方法。例如,小团体对此类数据很感兴趣。
请注意,并非所有东西都可能聚集。