O.rka
O.rka

Reputation: 30677

How to efficiently get edge weights from a networkx graph? (nx.Graph)

I'm trying to get the edge weights from a nx.Graph but the only way I could figure out iterates through the entire network. Is there a more efficient way to do this from networkx my nx.Graph assuming that I did not previously have the pd.DataFrame? This is for networkx ≥ 2.0.

import pandas as pd
import networkx as nx

# Load iris data
X_iris = pd.read_csv("https://pastebin.com/raw/dR59vTD4", sep="\t", index_col=0)

# Create correlation network
df_corr = X_iris.T.corr()

# Create graph from correlation network
graph_iris = nx.from_pandas_adjacency(df_corr, create_using=nx.Graph)

# Get edge weights
%time edge_weights = pd.Series({tuple(edge_data[:-1]):edge_data[-1]["weight"] for edge_data in graph_iris.edges(data=True)})
edge_weights

# CPU times: user 15.1 ms, sys: 98 µs, total: 15.2 ms
# Wall time: 15.2 ms
# iris_1    iris_0      0.995999
#           iris_2      0.996607
#           iris_3      0.997397
#           iris_4      0.992233
#           iris_5      0.993592
#                         ...   
# iris_146  iris_148    0.988469
#           iris_149    0.986481
# iris_147  iris_148    0.995708
#           iris_149    0.994460
# iris_148  iris_149    0.999916
# Length: 11175, dtype: float64

Upvotes: 2

Views: 11221

Answers (1)

yatu
yatu

Reputation: 88236

There isn't really a more performant way of doing so. Since the underlying data structure of a graph is a dictionary, and the weights are just a nested dictionary within it, you need to iterate over the edge data to get the weights. There is nx.get_edge_attributes, though you won't see a gain in performance, since it basically does the same (see source code):

def get_edge_attributes(G, name):
    # ...
    edges = G.edges(data=True)
    return dict( (x[:-1], x[-1][name]) for x in edges if name in x[-1] )

Upvotes: 3

Related Questions