如何在numpy中把标量索引转换为二维索引?

问题描述 投票:-1回答:1

假设我们有一组可以耦合的N个节点,就像在一个复杂的网络中一样,我们不关心链接的方向(所以1和2之间的链接和2和1一样)。

我想用一个numpy一维数组来表示每个链接的状态。状态取值为{1,0},其中1表示链接存在。这个数组,我们称它为 "状态",我想应该是N*(N-1)2长吧(自动循环除外)。

在这样的上下文中,我怎么能正确索引所有从节点a开始的链接,或者从节点b结束的链接呢?如果我们把数组称为 "状态",我会说state[i]=始于节点a的链接的状态,或者始于节点j的链接的状态。如果我们有N=10个节点,那么前8个条目对应于从节点1开始到节点2,3,4,...,10的链接,但是我找不到一个通用的方法来表达这个。谢谢。

P.s.。我知道二维矩阵可能更有用 但为了我的目的,我想用一个一维数组来解决这个问题。

python arrays numpy indexing adjacency-matrix
1个回答
0
投票

对节点(a,b)的索引可以使用由两个节点ID中较小的前面元素的数量决定的可变基数来计算。 将较大的那个加到这个基数上,就可以得到合适的索引。

你可以做一个函数来从一对节点标识符(基于零)获得状态索引,就像这样。

def indexOf(a,b,N=10):
    return indexOf(b,a,N) if a>b else N*a-a*(a+1)//2+b

按要求产生索引。

for a in range(10):
    print([indexOf(a,b) for b in range(10)])

[0, 1,  2,  3,  4,  5,  6,  7,  8,  9]
[1, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[2, 11, 19, 20, 21, 22, 23, 24, 25, 26]
[3, 12, 20, 27, 28, 29, 30, 31, 32, 33]
[4, 13, 21, 28, 34, 35, 36, 37, 38, 39]
[5, 14, 22, 29, 35, 40, 41, 42, 43, 44]
[6, 15, 23, 30, 36, 41, 45, 46, 47, 48]
[7, 16, 24, 31, 37, 42, 46, 49, 50, 51]
[8, 17, 25, 32, 38, 43, 47, 50, 52, 53]
[9, 18, 26, 33, 39, 44, 48, 51, 53, 54]

allPairs = [ (a,b) for a in range(10) for b in range(a,10) ]
print( sorted(allPairs,key=lambda ab:indexOf(*ab) ) )
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 6), (6, 7), (6, 8), (6, 9), (7, 7), (7, 8), (7, 9), (8, 8), (8, 9), (9, 9)]

请注意,这将为节点链接分配条目给自己

这种索引模型的缺点是,它要求事先知道节点数(N),并在每次调用时提供。

一个更通用的方法是基于一个不需要对N有一个预定值的递进,以不同的方式对平坦列表进行索引。

def indexOf(a,b):
    return indexOf(b,a) if a<b else a*(a+1)//2+b

可以像这样反过来(得到给定索引的节点对)。

def unindex(X):
    b = int( ((8*X+1)**0.5-1)/2 )
    a = X - b*(b+1)//2
    return a,b

索引的顺序不一样,但函数不需要知道N。

for a in range(10):
    print([indexOf(a,b) for b in range(10)])

[0,  1,  3,  6,  10, 15, 21, 28, 36, 45]
[1,  2,  4,  7,  11, 16, 22, 29, 37, 46]
[3,  4,  5,  8,  12, 17, 23, 30, 38, 47]
[6,  7,  8,  9,  13, 18, 24, 31, 39, 48]
[10, 11, 12, 13, 14, 19, 25, 32, 40, 49]
[15, 16, 17, 18, 19, 20, 26, 33, 41, 50]
[21, 22, 23, 24, 25, 26, 27, 34, 42, 51]
[28, 29, 30, 31, 32, 33, 34, 35, 43, 52]
[36, 37, 38, 39, 40, 41, 42, 43, 44, 53]
[45, 46, 47, 48, 49, 50, 51, 52, 53, 54]

print( [unindex(x) for x in range(N*(N+1)//2) ])
[(0, 0), (0, 1), (1, 1), (0, 2), (1, 2), (2, 2), (0, 3), (1, 3), (2, 3), (3, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (0, 5), (1, 5), (2, 5), (3, 5), (4, 5), (5, 5), (0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7), (0, 8), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8), (8, 8), (0, 9), (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9), (9, 9)]

使用numpy,如果你有一个节点对的数组,你可以通过写这样的函数(第二版)来获得它们对应的状态索引。

import numpy as np

def indexOf(ab):
    a,b = np.max(ab,axis=-1),np.min(ab,axis=-1)
    return a*(a+1)//2 + b

output:

N = 10

states = np.arange(N*(N+1)//2)%2        # some random node links

pairs = np.array( [[1,3],[2,4],[7,2]] ) # array of node pairs

connected= states[indexOf(pairs)]       # indirection to states

print(states)
# [0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]
print(indexOf(pairs))
# [ 7 12 30]
print(connected)
# [1 0 0]

如果你使用第一版的函数,你需要在每次调用时传递节点数。

def indexOf(ab,N=10):
    a,b = np.min(ab,axis=-1),np.max(ab,axis=-1)
    return N*a-a*(a+1)//2+b

connected= states[indexOf(pairs,N=10)]
© www.soinside.com 2019 - 2024. All rights reserved.