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