【问题标题】:Numpy cluster from connected graph来自连通图的 Numpy 集群
【发布时间】:2013-03-13 22:08:06
【问题描述】:

对连通图进行聚类的最佳方法是什么?

ex1:

[[ 1 1 1 1 0 0]
 [ 1 1 1 1 0 0]
 [ 1 1 1 1 0 0]
 [ 1 1 1 1 0 0]
 [ 0 0 0 0 1 1]
 [ 0 0 0 0 1 1]]

结果:

==> [[0,1,2,3],[4,5]]

ex2

[[ 0 1 0 1 0 0]
 [ 1 1 0 1 0 0]
 [ 0 1 0 1 0 0]
 [ 1 0 0 0 0 0]
 [ 0 0 1 0 1 1]
 [ 0 0 0 0 1 1]]

结果:

==> [[0,1,3],[2,4,5]]

ex3

[[ 0 1 0 0 0 0]
 [ 1 1 0 0 0 0]
 [ 0 0 1 1 0 0]
 [ 0 0 0 1 0 0]
 [ 0 0 0 0 1 1]
 [ 0 0 0 0 1 1]]

结果:

==> [[0,1],[2,3],[4,5]]

谢谢

【问题讨论】:

标签: python graph numpy


【解决方案1】:

在某些示例中,例如ex2,您给出了一个有向图或有向图,例如A != A.T。在这种情况下,可以通过考虑strongly connected components 找到更合理的定义。在这种情况下,拆分为[0,1,3],[4,5],[2]。 networkx可以帮你找到这些:

import numpy as np
import networkx as nx

A = np.array([[0,1,0,1,0,0],
              [1,1,0,1,0,0],
              [0,1,0,1,0,0],
              [1,0,0,0,0,0],
              [0,0,1,0,1,1],
              [0,0,0,0,1,1]])

G = nx.from_numpy_matrix(A, create_using=nx.DiGraph()) 
for subg in nx.strongly_connected_component_subgraphs(G):
    print subg.nodes()

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2019-10-17
    • 2013-10-24
    相关资源
    最近更新 更多