【问题标题】:How to find the n-largest edge weights in a network matrix (networkx)?如何在网络矩阵(networkx)中找到 n 最大的边权重?
【发布时间】:2016-12-20 16:25:20
【问题描述】:

我尝试使用以下代码生成网络矩阵。有了这个矩阵,我想找到不在对角线上的 20 个权重最高的边(i.e. i!=j 在矩阵中)。我还想获取由这些边组成的节点的名称(成对)。

import heapq
def mapper_network(self, _, info):
    G = nx.Graph()  #create a graph
    for i in range(len(info)):   
        edge_from = info[0]  # edge from
        edge_to = info[1]    # edge to
        weight = info[2]     # edge weight
        G.add_edge(edge_from, edge_to, weight=weight) #insert the edge to the graph
    A = nx.adjacency_matrix(G)  # create an adjacency matrix
    A_square = A * A  # find the product of the matrix
    print heapq.nlargest(20, A_square) # to print out the 20 highest weighted edges

但是,使用此代码,我未能生成 20 个权重最大的边。我得到raise ValueError("The truth value of an array with more than one " ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

相反,用这个

    print heapq.nlargest(20, range(len(A_square)), A_square.take)

它给了我:

    raise TypeError("sparse matrix length is ambiguous; use getnnz()"
    TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]

    def mapper_network(self, _, info):
    G = nx.Graph()
    for i in range(len(info)):
        edge_from = info[0]
        edge_to = info[1]
        weight = info[2]
        G.add_edge(edge_from, edge_to, weight=weight)
    A = nx.adjacency_matrix(G)
    A_square = A * A #can print (A_square.todense())
    weight = nx.get_edge_attributes(A_square, weight)
    edges = A_square.edges(data = True)
    s = sorted(G.edges(data=True), key=lambda (source, target, data): data['weight'])
    print s

我收到了

  File         "/tmp/MRQ7_trevor.vagrant.20160814.040827.770006/job_local_dir/1/mapper/0/mrjob.tar.gz/mrjob/job.py", line 433, in run
mr_job.execute()
 File "/tmp/MRQ7_trevor.vagrant.20160814.040827.770006/job_local_dir/1/mapper/0/mrjob.tar.gz/mrjob/job.py", line 442, in execute
self.run_mapper(self.options.step_num)
 File "/tmp/MRQ7_trevor.vagrant.20160814.040827.770006/job_local_dir/1/mapper/0/mrjob.tar.gz/mrjob/job.py", line 507, in run_mapper
for out_key, out_value in mapper(key, value) or ():
File "MRQ7_trevor.py", line 90, in mapper_network
weight = nx.get_edge_attributes(A_square, weight)
File "/home/vagrant/anaconda/lib/python2.7/site-packages/networkx/classes/function.py", line 428, in get_edge_attributes
if G.is_multigraph():
File "/home/vagrant/anaconda/lib/python2.7/site-packages/scipy/sparse/base.py", line 499, in __getattr__
raise AttributeError(attr + " not found")

AttributeError: is_multigraph not found

有人可以帮我解决这个问题吗?非常感谢!

【问题讨论】:

    标签: python matrix scipy heap networkx


    【解决方案1】:

    这一行的问题:

    heapq.nlargest(20, A_square)
    

    nlargest 不期望可迭代的可迭代对象,而是数字的可交互对象。

    所以你可以这样做:

    heapq.nlargest(20, itertools.chain.from_iterable(A_square))
    

    itertools.chain.iterables 接受一个可迭代的迭代,并使用所有内部迭代的内容创建一个新的迭代。


    但是,这并不能完全解决您最初的问题,原因有两个:

    1. 为什么要取邻接矩阵的平方?这样做只会为您提供图中长度为 2 的路径的最高加权和,这与您想要的完全不同。只需使用邻接矩阵即可。

    2. 在您的代码中没有任何地方可以删除对角线。你可以这样做:for n in G.number_of_nodes(): A[n][n] = 0

    【讨论】:

    • 非常感谢您的帮助。我取了邻接矩阵的平方,因为我想知道哪些节点“与共享节点连接最多”。例如,A-B、A-C,但!B-C。 B和C通过A连接。我希望通过这种方式知道哪对节点连接最多。我在 G.number_of_edges(): A[n][n] = 0 中尝试了 n,但它没有显示属性 number_of 边缘。我也希望知道节点名称(边)(即 A-C),但不知道如何使用稀疏的 csv 矩阵来做到这一点。谢谢!
    • 对不起,我的意思是number_of_nodes()(这是矩阵的大小)
    猜你喜欢
    • 2023-03-11
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-10
    • 2017-05-15
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多