NumPy:沿矩阵对角线构造正方形/展开对角线矩阵



假设您有两个数组:

index = [1, 2, 3]
counts = [2, 3, 2]

或奇异数组

arr = [1, 1, 2, 2, 2, 3, 3]

如何有效地构造矩阵

[
[1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 0, 3, 3]
]

NumPy吗?

我知道

square = np.zeros((7, 7))
np.fill_diagnol(square, arr) # see arr above

生产

[
[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 2, 0, 0, 0, 0],
[0, 0, 0, 2, 0, 0, 0],
[0, 0, 0, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 3]
]

如何展开

对于index[I]

指定的值,ncounts[index-1]时,n的对角线
tmp = np.array((arr * N)).reshape((len(arr), len(arr)) 
np.floor( (tmp + tmp.T) / 2 ) # <-- this is closer

array([[1., 1., 1., 1., 1., 2., 2.],
[1., 1., 1., 1., 1., 2., 2.],
[1., 1., 2., 2., 2., 2., 2.],
[1., 1., 2., 2., 2., 2., 2.],
[1., 1., 2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 3., 3.],
[2., 2., 2., 2., 2., 3., 3.]])

这得到了我想要的,但可能不是那么好缩放?

riffled = list(zip(index, counts))
riffled
# [(1, 2), (2, 3), (3, 2)]
a = np.zeros((len(arr), len(arr))) # 7, 7 square
last = 0 # <-- keep track of current sub square
for i, c in riffled:
a[last:last+c, last:last+c] = np.ones((c, c)) * i 
last += c # <-- shift square

产量

array([[1., 1., 0., 0., 0., 0., 0.],
[1., 1., 0., 0., 0., 0., 0.],
[0., 0., 2., 2., 2., 0., 0.],
[0., 0., 2., 2., 2., 0., 0.],
[0., 0., 2., 2., 2., 0., 0.],
[0., 0., 0., 0., 0., 3., 3.],
[0., 0., 0., 0., 0., 3., 3.]])

您可以使用scipy. linear。Block_diag:

import numpy as np
import scipy.linalg as linalg

a = 1*np.ones((2,2))
b = 2*np.ones((3,3))
c = 3*np.ones((2,2))
superBlock = linalg.block_diag(a,b,c)
print(superBlock)
#returns
#[[1. 1. 0. 0. 0. 0. 0.]
# [1. 1. 0. 0. 0. 0. 0.]
# [0. 0. 2. 2. 2. 0. 0.]
# [0. 0. 2. 2. 2. 0. 0.]
# [0. 0. 2. 2. 2. 0. 0.]
# [0. 0. 0. 0. 0. 3. 3.]
# [0. 0. 0. 0. 0. 3. 3.]]

如果你想从一个值列表和一个计数列表中获取,你可以这样做:

values = [1,2,3]
counts = [2,3,2]
mats = []
for v,c in zip(values,counts):
thisMatrix = v*np.ones((c,c))
mats.append( thisMatrix )

superBlock = linalg.block_diag(*mats)

print(superBlock)

这是一个通用的解决方案。

从索引/计数开始:

index = [1, 2, 1]
counts = [2, 3, 2]
arr = np.repeat(index, counts)
arr2 = np.repeat(range(len(index)), counts)
np.where(arr2 == arr2[:, None], arr, 0)

输出:

array([[1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1, 1]])

从数组版本开始:

arr = np.array([1, 1, 2, 2, 2, 1, 2])
arr2 = np.cumsum(np.diff(arr,prepend=np.nan) != 0)
np.where(arr2 == arr2[:, None], arr, 0)

输出:

array([[1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 2]])

尝试广播:

idx = np.repeat(np.arange(len(counts)), counts)
np.where(idx==idx[:,None], arr, 0)
# or
# arr * (idx==idx[:,None])

输出;

array([[1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 2, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 0, 3, 3]])

相关内容

  • 没有找到相关文章

最新更新