NetworkX - 创建反转图的深层副本

时间:2014-11-01 21:50:24

标签: python graph networkx

我在尝试在NetworkX中创建反向图的深层副本并检查该副本的PageRank时遇到问题。似乎问题不在于PageRank,它只是在尝试计算破坏的反向图的PageRank时抛出异常。

首先,我使用这个代码生成一个有向图,它基本上只创建一堆节点,随机选取其中的一些,然后从该节点绘制有向边到另外50个随机选择的节点。然后它会修剪0度的所有节点。

def global_classroom(nodes):
    network = nx.DiGraph()
    network.add_nodes_from([User(i, "0.0.1") for i in range(nodes)])
    network_nodes = network.nodes()
    teachers = random.sample(network_nodes, nodes / 100)
    for teacher in teachers:
        for i in range(nodes / 20):
            student = network_nodes[random.randrange(nodes)]
            if teacher != student:
            network.add_edge(teacher, student)
    for node in network_nodes:
        if network.degree(node) == 0:
            network.remove_node(node)
    return network

现在,如果我这样称呼:

G = global_classroom(1000)
G.reverse(False)
print nx.pagerank(G)

一切都很完美。 NetworkX能够计算PageRank没问题。

但如果我称之为:

G = global_classroom(1000)
G_page_rank = G.reverse()
nx.pagerank(G_page_rank)

抛出异常

  Traceback (most recent call last):
  File "a.py", line 62, in <module>
  rank = nx.pagerank(L_page_rank)
  File "<string>", line 2, in pagerank
  File "/Users/PSnyder/Documents/Git/infection/networkx/utils/decorators.py", line 68, in _not_implemented_for
  return f(*args,**kwargs)
  File "/Users/PSnyder/Documents/Git/infection/networkx/algorithms/link_analysis/pagerank_alg.py", line 140, in pagerank
  dangling_nodes = [n for n in W if W.out_degree(n, weight=weight) == 0.0]
  File "/Users/PSnyder/Documents/Git/infection/networkx/classes/digraph.py", line 1023, in out_degree
  return next(self.out_degree_iter(nbunch,weight))[1]
  File "/Users/PSnyder/Documents/Git/infection/networkx/classes/digraph.py", line 941, in out_degree_iter
  for n,nbrs in nodes_nbrs:
  File "/Users/PSnyder/Documents/Git/infection/networkx/classes/digraph.py", line 934, in <genexpr>
  nodes_nbrs=((n,self.succ[n]) for n in self.nbunch_iter(nbunch))
  KeyError: <__main__.User object at 0x104618050>

<__main__.User object at 0x104618050>是构成我的有向图中节点的众多对象之一。

我希望能够简单地制作一份深层副本,而不是需要再次反转我的图形以将其恢复为原始形式。

以下是构成我的节点的User类的全部内容:

class User(object):
    def __init__(self, name, version, teachers = [], students = []):
        self.name = name
        self.version = version
        G.add_node(self)
        self.add_teachers(teachers)
        self.add_students(students)

    def add_students(self, students):
        for student in students:
            G.add_edge(self, student)

    def add_teachers(self, teachers):
        for teacher in teachers:
            G.add_edge(teacher, self)

1 个答案:

答案 0 :(得分:1)

也许您的User对象导致了问题?你没有包含它,所以我无法看到这是否是一个networkx错误。

这有效(使用整数而不是User())

import networkx as nx
import random

def global_classroom(nodes):
    network = nx.DiGraph()
    network.add_nodes_from([i for i in range(nodes)])
    network_nodes = network.nodes()
    teachers = random.sample(network_nodes, nodes / 100)
    for teacher in teachers:
        for i in range(nodes / 20):
            student = network_nodes[random.randrange(nodes)]
            if teacher != student:
                network.add_edge(teacher, student)
    for node in network_nodes:
        if network.degree(node) == 0:
            network.remove_node(node)
    return network


if __name__=='__main__':
    G = global_classroom(1000)
    G.reverse(False)
    print nx.pagerank(G).items()[0:5] # first 5