我正在嘗試為圖中節點的每個子集分配一個熱編碼。下面是我正在嘗試的代碼import networkx as nximport numpy as npgraph=nx.karate_club_graph()nodes=list(graph.nodes())n=graph.number_of_nodes()subset_nodes=[1,2]for v in subset_nodes: y=nodes.index(v) prob_vec=np.zeros((n,n)) prob_vec[0][y]=1 print(prob_vec)我得到這個結果[0. 1. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.]][[0. 0. 1. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.]]I expect a matrix, with the subset nodes rows contains one hot encoding(1 value for each node in the subset node and others being zeros) like below:[0. 1. 0. ... 0. 0. 0.] [0.0 . 1. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.]]任何幫助都感激不盡
1 回答

小唯快跑啊
TA貢獻1863條經驗 獲得超2個贊
如果我明白你想要做什么,我認為你需要稍微調整你的代碼。您當前正在打印每個循環并將每個循環的 prob_vec 重置為 0。我認為你想做更多這樣的事情:
import networkx as nx
import numpy as np
graph=nx.karate_club_graph()
nodes=list(graph.nodes())
n=graph.number_of_nodes()
subset_nodes=[1,2]
prob_vec=np.zeros((n,n))
for v in range(n):
y = nodes.index(v)
if y in subset_nodes:
prob_vec[v][y]=1
print(prob_vec)
這輸出:
[[0. 0. 0. ... 0. 0. 0.]
[0. 1. 0. ... 0. 0. 0.]
[0. 0. 1. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
添加回答
舉報
0/150
提交
取消