【问题标题】:Python/NetworkX: calculate edge weights on the flyPython/NetworkX:动态计算边权重
【发布时间】:2017-04-20 20:58:57
【问题描述】:

我有一个使用networkx 创建的未加权 图,我想根据边出现的计数/频率计算节点之间边的权重。我的图表中的一条边可以出现不止一次,但边出现的频率是事先不知道的。目的是根据连接节点之间移动的权重(例如计数/频率)来可视化边缘。本质上,我想创建一个连接节点之间移动的网络流量图,并根据颜色或边缘宽度进行可视化。例如,从节点 0 到 1 的边之间有 10 次移动,而节点 1 到 2 有 5 次移动,因此边 0-1 将使用不同的边颜色/大小进行可视化。

如何动态计算两个节点之间的边的权重(在使用g.add_edges_from() 将它们添加到图表之后),然后重新应用到我的图表以进行可视化?下面是我最初用于创建图表的图表、数据和代码的示例,以及我尝试失败的解决方案。

图表

样本数据

簇质心(节点)

cluster_label,latitude,longitude
0,39.18193382,-77.51885109
1,39.18,-77.27
2,39.17917928,-76.6688633
3,39.1782,-77.2617
4,39.1765,-77.1927
5,39.1762375,-76.8675441
6,39.17468,-76.8204499
7,39.17457332,-77.2807235
8,39.17406072,-77.274685
9,39.1731621,-77.2716502
10,39.17,-77.27

轨迹(边)

user_id,trajectory
11011.0,"[[340, 269], [269, 340]]"
80973.0,"[[398, 279]]"
608473.0,"[[69, 28]]"
2139671.0,"[[382, 27], [27, 285]]"
3945641.0,"[[120, 422], [422, 217], [217, 340], [340, 340]]"
5820642.0,"[[458, 442]]"
6060732.0,"[[291, 431]]"
6912362.0,"[[68, 27]]"
7362602.0,"[[112, 269]]"
8488782.0,"[[133, 340], [340, 340]]"

代码

import csv
import networkx as nx
import pandas as pd
import community
import matplotlib.pyplot as plt
import time
import mplleaflet

g = nx.MultiGraph()

df = pd.read_csv('cluster_centroids.csv', delimiter=',')
df['pos'] = list(zip(df.longitude,df.latitude))
dict_pos = dict(zip(df.cluster_label,df.pos))
#print dict_pos

for row in csv.reader(open('edges.csv', 'r')):
    if '[' in row[1]:       #
        g.add_edges_from(eval(row[1]))

# Plotting with mplleaflet
fig, ax = plt.subplots()
nx.draw_networkx_nodes(g,pos=dict_pos,node_size=50,node_color='b')
nx.draw_networkx_edges(g,pos=dict_pos,linewidths=0.01,edge_color='k', alpha=.05)
nx.draw_networkx_labels(g,dict_pos)
mplleaflet.show(fig=ax.figure)

我尝试使用g.add_weighted_edges_from() 并添加weight=1 作为属性,但没有任何运气。我也试过用这个也没有用:

for u,v,d in g.edges():
    d['weight'] = 1
g.edges(data=True)
edges = g.edges()
weights = [g[u][v]['weight'] for u,v in edges]

【问题讨论】:

    标签: python graph networkx


    【解决方案1】:

    由于没有得到答复,因此打开了关于此主题的第二个问题(此处:Python/NetworkX: Add Weights to Edges by Frequency of Edge Occurance)并收到了回复。根据边缘出现的计数为边缘添加权重:

    g = nx.MultiDiGraph()
    
    df = pd.read_csv('G:\cluster_centroids.csv', delimiter=',')
    df['pos'] = list(zip(df.longitude,df.latitude))
    dict_pos = dict(zip(df.cluster_label,df.pos))
    #print dict_pos
    
    
    for row in csv.reader(open('G:\edges.csv', 'r')):
        if '[' in row[1]:       #
            g.add_edges_from(eval(row[1]))
    
    for u, v, d in g.edges(data=True):
        d['weight'] = 1
    for u,v,d in g.edges(data=True):
        print u,v,d
    

    根据上述计数缩放颜色和边缘宽度:

    minLineWidth = 0.25
    
    for u, v, d in g.edges(data=True):
        d['weight'] = c[u, v]*minLineWidth
    edges,weights = zip(*nx.get_edge_attributes(g,'weight').items())
    
    values = range(len(g.edges()) 
    jet = cm = plt.get_cmap('YlOrRd')
    cNorm  = colors.Normalize(vmin=0, vmax=values[-1])
    scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)
    colorList = []
    
    for i in range(len(g.edges()):
        colorVal = scalarMap.to_rgba(values[i])
        colorList.append(colorVal)
    

    并将width=[d['weight'] for u,v, d in g.edges(data=True)]edge_color=colorList 作为nx.draw_networkx_edges() 中的参数传递

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2014-05-22
      相关资源
      最近更新 更多