Код: Выделить всё
import numpy as np
import networkx as nx
np.random.seed(123)
n_samples = 10
uv = [
(1, 2),
(2, 3),
(3, 4),
(4, 5),
(5, 6)
]
G = nx.MultiDiGraph()
for u, v in uv:
weights = np.random.uniform(0, 1, size=n_samples)
G.add_edges_from([(u, v, dict(sample_id=s+1, weight=weights[s])) for s in range(n_samples)])
A = nx.to_numpy_array(G=G, nodelist=list(G.nodes))
Поэтому выходные данные выглядеть следующим образом:
Код: Выделить всё
[[0. 5.44199353 0. 0. 0. 0. ]
[0. 0. 4.12783997 0. 0. 0. ]
[0. 0. 0. 5.37945594 0. 0. ]
[0. 0. 0. 0. 4.95418265 0. ]
[0. 0. 0. 0. 0. 5.18942126]
[0. 0. 0. 0. 0. 0. ]]
Код: Выделить всё
print(A.shape)
>> (6, 6, 10)
Подробнее здесь: https://stackoverflow.com/questions/793 ... ltidigraph
Мобильная версия