Add vector weighted edges with networkx

Viewed 30

I am facing a problem for which I cannot find a solution. I need to work with a graph data structure, more precisely a weighted oriented graph. But I wish for the weights, I wish not to add a float but a vector (for example an array numpy)

For example :

import networkx as nx

Graph = nx.DiGraph()

Graph.add_edge("Alpha", "Beta", weight=0.3)
Graph.add_edge("Beta", "Alpha", weight=0.9)
Graph.add_edge("Alpha", "Omega", weight=0.4)

A = nx.to_numpy_array(Graph)

print(A.shape)

It's work, ok, and I have (3,3) shape, because I have 3 vertices, ok. But, I want this :

Graph = nx.DiGraph()

Graph.add_edge("Alpha", "Beta", weight=np.array([0.5, 0.3, 0.2, 0.3, 0.5]))
Graph.add_edge("Beta", "Alpha", weight=np.array([0.5, 0.3, 0.2, 0.3, 0.5]))
Graph.add_edge("Alpha", "Omega", weight=np.array([0.5, 0.3, 0.2, 0.3, 0.5]))

A = nx.to_numpy_array(Graph)

print(A.shape)

I need a (3,3,5) tensor, I need "to weight" each edge by a vector of 5 values. And I have this error : "ValueError: shape mismatch: value array of shape (3,5) could not be broadcast to indexing result of shape (3,)"

Does anyone have a solution please?

0 Answers
Related