python - 如何找到网络矩阵(networkx)中n最大的边权重?

标签 python matrix scipy heap networkx

我尝试使用以下代码来生成网络矩阵。通过这个矩阵,我想找到不在对角线上的 20 个最高权重边(即矩阵中的 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 true value of an array with more one " ValueError:具有多个元素的数组的真值不明确。使用a.any()或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")

属性错误:未找到 is_multigraph

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

最佳答案

这一行的问题:

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

关于python - 如何找到网络矩阵(networkx)中n最大的边权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38938571/

相关文章:

python - 获得 kolmogorov-smirnov 检验所需的临界值

python - 如何深入字典并删除最深的键级别

python - 如何在其默认程序中启动文件,然后在脚本完成时关闭它?

c - 来自文本文件c代码的邻接矩阵

android - 在ANDROID编程中从位图图像复制位矩阵

python - Scipy.optimize.fmin powell 直接参数语法

Python:找到最小的整数

Python】将两个文本文件合二为一(逐行)

R 高斯消除和 qr 分解

Python - 在函数中使用来自另一个模块的函数