基于图的边缘的相应分数 [英] Corresponding scores based on edges of a graph

查看:78
本文介绍了基于图的边缘的相应分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import numpy as np

score = np.array([
    [0.9, 0.7, 0.2, 0.6, 0.4],
    [0.7, 0.9, 0.6, 0.8, 0.3],
    [0.2, 0.6, 0.9, 0.4, 0.7],
    [0.6, 0.8, 0.4, 0.9, 0.3],
    [0.4, 0.3, 0.7, 0.3, 0.9]])

l2= [(3, 5), (1, 4), (2, 3), (3, 4), (2, 5), (4, 5)]

我想基于边缘列表获取相应的分数向量.score数组表示邻接矩阵,其中score(1,2)表示边缘(1,2)

I want to get the corresponding scores vector based on the edge list.The score array represents an adjacency matrix where score(1,2) represents the edge (1,2)

OUTPUT:

[0.7 0.6 0.6 0.4 0.3 0.3]

推荐答案

我们可以将包含索引的元组列表转换为数组,然后使用slicing或元组打包的元组.

We could convert the list of tuples holding the indices to array and then use slicing or tuple packed ones.

因此,转换为array:

So, convert to array :

l2_arr = np.asarray(l2)-1

然后,一种方法是-

score[l2_arr[:,0], l2_arr[:,1]]

另一个-

score[tuple(l2_arr.T)]

为了完整起见,这里使用循环理解来提取行,列索引,从而避免任何数组转换-

For completeness, here's one using a loop-comprehension to extract the row, column indices and thus avoiding any array conversion -

score[[i[0]-1 for i in l2], [i[1]-1 for i in l2]]

这篇关于基于图的边缘的相应分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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