我有使用 networkx
包生成的2个 大型 网络,它们分别为 G
和 G1
。我想计算所有节点之间的 Jaccard相似度指数。
一种可能的方法如下:
def returnJaccardNetworks(G, G1):
tmp = list(G.nodes())
tmp1 = list(G1.nodes())
tmp2 = np.unique([tmp, tmp1]) ### Find nodes in the networks
jc = []
for i in tmp2:
## if the node i is in G and in G1 compute
## the similarity between the lists of the ajacent nodes
## otherwise append 0
if (i in G) and (i in G1):
k1 = list(G[i]) ## adjacent nodes of i in the network G
k2 = list(G1[i]) ## adjacent nodes of i in the network G1
### Start Jaccard Similarity
intersect = list(set(k1) & set(k2))
n = len(intersect)
jc.append(n / float(len(k1) + len(k2) - n))
### End Jaccard Similariy
else:
jc.append(0)
return jc
我想知道是否有更有效的方法。我注意到包中有一个名为jaccard_coefficient
的函数,但我不确定它是如何工作的。