How to write a code for link prediction precision assessment in python?

与世无争的帅哥 提交于 2020-01-14 06:42:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!