如何在python中编写用于链接预测精度评估的代码? [英] How to write a code for link prediction precision assessment in python?

查看:797
本文介绍了如何在python中编写用于链接预测精度评估的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用adamic_adar索引进行链接预测问题.数据集是一个网格网络(具有1000个链接的边列表).我从观察到的数据集中随机选择了80%(800)的边缘.我需要从如下所示的Pred中选择最高的200条预测链接,并计算出精确率.我不知道下一步该怎么做.我该怎么办..帮助!

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)

推荐答案

我假设u,v是图形的顶点,p是精度.

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)

这篇关于如何在python中编写用于链接预测精度评估的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆