给定一个(可能很大~2+GB)json 文件中的节点之间的交易,有~百万个节点和~1000 万个交易,每个交易有 10-1000 个节点,例如
{"transactions":
[
{"transaction 1": ["node1","node2","node7"], "weight":0.41},
{"transaction 2": ["node4","node2","node1","node3","node10","node7","node9"], "weight":0.67},
{"transaction 3": ["node3","node10","node11","node2","node1"], "weight":0.33},...
]
}
将其转换为节点亲和性矩阵的最优雅、最有效的 pythonic 方法是什么,其中亲和性是节点之间加权交易的总和。
affinity [i,j] = weighted transaction count between nodes[i] and nodes[j] = affinity [j,i]
例如
affinity[node1, node7] = [0.41 (transaction1) + 0.67 (transaction2)] / 2 = affinity[node7, node1]
注意:亲和矩阵是对称的,因此仅计算下三角就足够了。
值不代表***结构示例!
节点 1 |节点2 |节点 3 |节点4 | ....
node1 1 .4 .1 .9 ...
node2 .4 1 .6 .3 ...
node3 . . 6 1 .7 ...< br/> node4 .9 .3 .7 1 ...
...
请您参考如下方法:
首先我会清理数据并用一个整数表示每个节点并从这样的字典开始
data=[{'transaction': [1, 2, 7], 'weight': 0.41},
{'transaction': [4, 2, 1, 3, 10, 7, 9], 'weight': 0.67},
{'transaction': [3, 10, 11, 2, 1], 'weight': 0.33}]
不确定这是否足够 pythonic 但它应该是不言自明的
def weight(i,j,data_item):
return data_item["weight"] if i in data_item["transaction"] and j in data_item["transaction"] else 0
def affinity(i,j):
if j<i: # matrix is symmetric
return affinity(j,i)
else:
weights = [weight(i,j,data_item) for data_item in data if weight(i,j,data_item)!=0]
if len(weights)==0:
return 0
else:
return sum(weights) / float(len(weights))
ln = 10 # number of nodes
A = [[affinity(i,j) for j in range(1,ln+1)] for i in range(1,ln+1)]
查看亲和度矩阵
import numpy as np
print(np.array(A))
[[ 0.47 0.47 0.5 0.67 0. 0. 0.54 0. 0.67 0.5 ] [ 0.47 0.47 0.5 0.67 0. 0. 0.54 0. 0.67 0.5 ] [ 0.5 0.5 0.5 0.67 0. 0. 0.67 0. 0.67 0.5 ] [ 0.67 0.67 0.67 0.67 0. 0. 0.67 0. 0.67 0.67] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0.54 0.54 0.67 0.67 0. 0. 0.54 0. 0.67 0.67] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] [ 0.67 0.67 0.67 0.67 0. 0. 0.67 0. 0.67 0.67] [ 0.5 0.5 0.5 0.67 0. 0. 0.67 0. 0.67 0.5 ]]
