【问题标题】:improve python performance in networkx提高networkx中的python性能
【发布时间】:2013-06-20 23:47:29
【问题描述】:

我正在使用包numpy 和网络在python 中创建一个网络。这是我需要帮助的代码:

def create_rt_network(self):                                                                                                       
    """construct a retweet network from twitter db"""                                                                                                                                                                        
    con = mdb.connect(**proper-information**)                                                                                                                                                                                            
    cur = con.cursor(mdb.cursors.DictCursor)                                                                                       
    cur.execute("select COUNT(*) from users")                                                                                                                                                                                                                   
    N = cur.fetchone()['COUNT(*)']                                                                                                                                                                                                                                       
    mat = np.empty((N, N))                                                                                                                                                                                                                                  
    #read adjacency table and store data into mat                                                                                                                                                                                                                 
    cur.execute("select * from adjacency")                                                                                                                                                                               
    rows = cur.fetchall() 

    for row in rows:                                                                                                                                                             
        curRow = row['r']                                                                                                                                                                                                                                   
        curCol = row['c']                                                                                                                                                         
        weight = row['val']                                                                                                                                                                                                                                               
        mat[curRow][curCol] = weight                                                                                                                                                                                                                                                                                                                                                                          
    cur.close()                                                                                                                                                                                              
    con.close()      

    g = nx.from_numpy_matrix(mat, create_using=nx.DiGraph())                                                                                            
    return g 

事实:

  1. 创建此图表大约需要一个小时
  2. adjacency 包含 212,000 行

由于我是 python 新手,我不知道解释器执行了多少优化(如果有的话)。无论如何,我认为错误实际上是在行中创建图表:

g = nx.from_numpy_matrix(mat, create_using=nx.DiGraph())

我相信这是因为:

  1. 我已经运行了没有该行的代码,而且速度很快(最多 10 秒)
  2. 我认为写 mat 是 O(nlgn),因为我们有 n 行,从数据库中读取(btree 搜索)是 O(lgn),写 mat 是 O(1)。

我只是想到读取邻接矩阵需要 O(n^2) 时间;也许邻接列表(在networkx 中实现为字典的字典)会更快。在那种情况下,有人知道networkx中的加权图和邻接表吗?

如果您想了解更多信息,请告诉我,非常感谢所有帮助! 注意:未来:我如何知道一个小时是否合理?

【问题讨论】:

  • 你试过分析它吗? pythonhosted.org/line_profiler
  • 先尝试手动查找瓶颈在哪里。是在nx.from_numpy_matrix() 还是循环中?
  • 绝对是nx.from_numpy_matrix()。如果没有该语句,它最多运行 10 秒。

标签: python performance numpy networkx


【解决方案1】:

我不确定为什么将 numpy 矩阵转换为 Di-Graph 时速度很慢。请在下面尝试这种方法,看看是否有帮助。

def create_directed_graph(rows):
    g = nx.DiGraph()
    for row in rows:
        curRow = row['r']
        curCol = row['c']
        weight = row['val']
        g.add_edge(curRow,curCol,Weight=weight)
    return g

【讨论】:

  • 那是我的愚蠢!从某种意义上说,我是对的,读取邻接矩阵确实需要很长时间 O(n^2)。
猜你喜欢
  • 2011-09-15
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多