问题
I am doing a link prediction problem using the adamic_adar index. The dataset is a grid network(edgelist with 1000 links). I randomly selected 80% (800) of the edges from the observed dataset. I need to select the highest 200 predicted links from preds as below and also calculate the precision ratio. I dont know what to do next. How would I do..help!
import numpy as np
import networkx as nx
G = nx.read_edgelist('Grid.txt', create_using=nx.Graph(), nodetype=int)
preds = nx.adamic_adar_index(G);
for u, v, p in preds:
'(%d, %d) -> %.8f' % (u, v, p)
print(u, v, p)
回答1:
I assume u, v to be the vertex of the graph, and p be the precision.
import numpy as np
import networkx as nx
import random
G = nx.read_edgelist('Grid.txt', create_using=nx.Graph(), nodetype=int)
preds = nx.adamic_adar_index(G)
preds = random.sample(preds, int(len(preds)*0.8))
preds = sorted(preds, key=lambda x: x[2], reverse=True)[:200]
ratio = sum([t[2] for t in preds])/len(preds)
print(ratio)
来源:https://stackoverflow.com/questions/54147235/how-to-write-a-code-for-link-prediction-precision-assessment-in-python