假設您有兩個陣列:
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]
]
我如何“擴展”對角線由nwhere niscounts[index-1]指定的值index[I]
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.]])
uj5u.com熱心網友回復:
您可以使用 scipy.linalg.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)
uj5u.com熱心網友回復:
嘗試廣播:
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]])
uj5u.com熱心網友回復:
這是一個通用的解決方案。
從索引/計數開始:
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]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338372.html
上一篇:查找具有nan值的行并將其洗掉
下一篇:Python矩陣行移位(也列)
