Zachary 空手道俱乐部是一个被广泛使用的社交网络,其中的节点代表空手道俱乐部的成员,边代表成员之间的相互关系。当年,Zachary 在研究空手道俱乐部的时候,管理员和教员发生了冲突,导致俱乐部一分为二。下图显示了该网络的图表征,其中的节点标注是根据节点属于俱乐部的哪个部分而得到的,「0」表示属于Mr. Hi部分的中心节点,[32」表示属于Officer阵营的中心节点
通过networkx调用karate club图
import networkx as nx
import numpy as np
#通过nx.to_numpy_array将图节点按照顺序转化成numpy的数组
zkc=nx.karate_club_graph()
order=list(zkc.nodes())
A=nx.to_numpy_array(zkc.nodes(),nodelist=order)
计算GCN的各项变量
具体算法原理见上一篇博客
#具体算法原理见上一篇博客
I=np.eye(A.shape[0])
A_hat=A+I
print('A_hat:',A_hat)
D_hat=np.sum(A_hat,axis=0)
D_hat=np.diag(D_hat)
print('D_hat:',D_hat)
D_hat_I=np.linalg.inv(D_hat)
D_hat_I_=D_hat_I**0.5
D_hat_I_=np.matrix(D_hat_I_)
A_hat=np.matrix(A_hat)
print('D_hat_I:',D_hat_I)
print('D_hat_I_:',D_hat_I_)
>>>A: [[0. 1. 1. ... 1. 0. 0.]
[1. 0. 1. ... 0. 0. 0.]
[1. 1. 0. ... 0. 1. 0.]
...
[1. 0. 0. ... 0. 1. 1.]
[0. 0. 1. ... 1. 0. 1.]
[0. 0. 0. ... 1. 1. 0.]]
A_hat: [[1. 1. 1. ... 1. 0. 0.]
[1. 1. 1. ... 0. 0. 0.]
[1. 1. 1. ... 0. 1. 0.]
...
[1. 0. 0. ... 1. 1. 1.]
[0. 0. 1. ... 1. 1. 1.]
[0. 0. 0. ... 1. 1. 1.]]
D_hat: [[17. 0. 0. ...