什么是在2D numpy数组中对角插入元素的最快方法?
假设我们有一个像2D numpy的数组:什么是在2D numpy数组中对角插入元素的最快方法?
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]]
我想插入值0说这样对角,它变成了:
matrix = [[0, 1, 2, 3],
[4, 0, 5, 6],
[7, 8, 0, 9],
[10, 11, 12, 0]]
什么是做到这一点的最快的方法?
创建一个新的更大的矩阵,剩下零空间。原来矩阵复制到一个子矩阵,剪辑,重塑:
matrix = numpy.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]])
matrix_new = numpy.zeros((4,5))
matrix_new[:-1,1:] = matrix.reshape(3,4)
matrix_new = matrix_new.reshape(-1)[:-4].reshape(4,4)
或更广义的形式:
matrix = numpy.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]])
d = matrix.shape[0]
assert matrix.shape[1] == d - 1
matrix_new = numpy.ndarray((d, d+1), dtype=matrix.dtype)
matrix_new[:,0] = 0
matrix_new[:-1,1:] = matrix.reshape((d-1, d))
matrix_new = matrix_new.reshape(-1)[:-d].reshape(d,d)
这里有一种方法(但我不能保证,这是最快的方法) :
In [62]: a
Out[62]:
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
In [63]: b = np.zeros((a.shape[0], a.shape[1]+1), dtype=a.dtype)
In [64]: i = np.arange(b.shape[0])
In [65]: j = np.arange(b.shape[1])
In [66]: b[np.not_equal.outer(i, j)] = a.ravel() # or a.flat, if a is C-contiguous
In [67]: b
Out[67]:
array([[ 0, 1, 2, 3],
[ 4, 0, 5, 6],
[ 7, 8, 0, 9],
[10, 11, 12, 0]])
它适用于任何2-d阵列a
:
In [72]: a
Out[72]:
array([[17, 18, 15, 19, 12],
[16, 14, 11, 16, 17],
[19, 11, 16, 11, 14]])
In [73]: b = np.zeros((a.shape[0], a.shape[1]+1), dtype=a.dtype)
In [74]: i = np.arange(b.shape[0])
In [75]: j = np.arange(b.shape[1])
In [76]: b[np.not_equal.outer(i, j)] = a.flat
In [77]: b
Out[77]:
array([[ 0, 17, 18, 15, 19, 12],
[16, 0, 14, 11, 16, 17],
[19, 11, 0, 16, 11, 14]])
它有效,但我认为@丹尼尔的答案是要走的路。
你可以用'〜np.eye(* b.shape,dtype = np.bool)替换'np.not_equal.outer(i,j)' –
另一种方法,可能速度较慢,与追加和重塑
import numpy as np
mat = np.array(range(1,13)).reshape(4,3)
mat
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
z=np.zeros((3,1), dtype=mat.dtype)
m3=np.append(z,mat.reshape(3,4),1)
np.append(m3,0).reshape(4,4)
array([[ 0, 1, 2, 3],
[ 4, 0, 5, 6],
[ 7, 8, 0, 9],
[10, 11, 12, 0]])
即将发布的内容非常接近这个!尼斯想到这里。另外,看看使用'np.hstack'和'np.concatenate'是否有助于提升性能。 – Divakar
同样,我不知道它是多么快,但你可以尝试使用numpy.lib.stride_tricks.as_strided
:
import numpy as np
as_strided = np.lib.stride_tricks.as_strided
matrix = (np.arange(12)+1).reshape((4,3))
n, m = matrix.shape
t = matrix.reshape((m, n))
t = np.hstack((np.array([[0]*m]).T, t))
t = np.vstack((t, [0]*(n+1)))
q = as_strided(t, (n,n), (t.itemsize*n, 8))
print(q)
输出:
[[ 0 1 2 3]
[ 4 0 5 6]
[ 7 8 0 9]
[10 11 12 0]]
也就是说,用零重新填充重构阵列的左和底部并跨步到将左手零点放在对角线上。不幸的是,在(n+1,n)
数组的情况下(因为例如5 * 3 = 15小于4 * 4 = 16),您需要底部一行零以获得输出矩阵中的最终零。如果你以方矩阵开始,你可以不用这个。
看起来你正在采取下三角阵和上三角阵,并用零对角线分隔它们。该序列确实是:
In [54]: A=np.arange(1,13).reshape(4,3)
目标阵列,具有一个多个列
In [55]: B=np.zeros((A.shape[0],A.shape[1]+1),dtype=A.dtype)
复印在下部三(没有对角线)
In [56]: B[:,:-1]+=np.tril(A,-1)
复印上三
In [57]: B[:,1:]+=np.triu(A,0)
In [58]: B
Out[58]:
array([[ 0, 1, 2, 3],
[ 4, 0, 5, 6],
[ 7, 8, 0, 9],
[10, 11, 12, 0]])
有一些np.tril_indices...
函数,但它们只能用于方阵。所以他们不能与A
一起使用。
比方说你有apxq numpy的2D阵列A,这里是与(P,Q)作为样品(3,4):
In []: A = np.arange(1,13).reshape(4,3)
In []: A
Out[]:
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
Step 1:
要插入对角线零,它需要制作一个新的形状为px q + 1的二维数组。
在此之前,我们创建与非对角线元素的列索引值的2D阵列的新的二维阵列是这样
In []: columnIndexArray = np.delete(np.meshgrid(np.arange(q+1), np.arange(p))[0], np.arange(0, p * (q+1), q+2)).reshape(p,q)
的输出上述将如下所示:
In []: columnIndexArray
Out[]:
array([[1, 2, 3],
[0, 2, 3],
[0, 1, 3],
[0, 1, 2]])
Step 2:
现在构造PX q + 1个2d中的零的这样
阵列In []: B = np.zeros((p,q+1))
In []: B
Out[]:
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
Step 3:
现在从A
In []: B[np.arange(p)[:,None], columnIndexArray] = A
In []: B
Out[]:
array([[ 0., 1., 2., 3.],
[ 4., 0., 5., 6.],
[ 7., 8., 0., 9.],
[ 10., 11., 12., 0.]])
Note:
分配与所述值的非对角元素要使代码动态与A.shape [0]和q与A.shape替换p [1]。
形状总是'(4,3)'?有什么限制吗?如果您尝试在形状为((3,4))的阵列中插入对角线,您想要什么? – mgilson
原始形状总是像这样(n + 1,n),其中n> 0,但我更喜欢如果答案也适用于(n,n)。 –
我会创建一个新的数组(零),并使用'np.tri ...'函数将旧数组的上下三角形复制到新数组中。 – hpaulj