本文介绍了删除numpy数组的对角元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
需要输出:
array([[2, 3],
[4, 6],
[7, 8]])
使用迭代或循环很容易做到这一点,但是应该有一种简洁的方法可以不使用循环来做到这一点.谢谢
It is easy to use iteration or loop to do this, but there should be a neat way to do this without using loops. Thanks
推荐答案
方法1
使用masking
-
A[~np.eye(A.shape[0],dtype=bool)].reshape(A.shape[0],-1)
样品运行-
In [395]: A
Out[395]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [396]: A[~np.eye(A.shape[0],dtype=bool)].reshape(A.shape[0],-1)
Out[396]:
array([[2, 3],
[4, 6],
[7, 8]])
方法2
使用非对角元素的常规模式,可以通过范围数组的广播加法来跟踪-
Using the regular pattern of non-diagonal elements that could be traced with broadcasted additions with range arrays -
m = A.shape[0]
idx = (np.arange(1,m+1) + (m+1)*np.arange(m-1)[:,None]).reshape(m,-1)
out = A.ravel()[idx]
方法3 (大步向前!)
滥用先前方法中的非对角元素的常规模式,我们可以引入 np.lib.stride_tricks.as_strided
和一些slicing
帮助,例如-
Abusing the regular pattern of non-diagonal elements from previous approach, we can introduce np.lib.stride_tricks.as_strided
and some slicing
help, like so -
m = A.shape[0]
strided = np.lib.stride_tricks.as_strided
s0,s1 = A.strides
out = strided(A.ravel()[1:], shape=(m-1,m), strides=(s0+s1,s1)).reshape(m,-1)
运行时测试
作为功能的方法:
def skip_diag_masking(A):
return A[~np.eye(A.shape[0],dtype=bool)].reshape(A.shape[0],-1)
def skip_diag_broadcasting(A):
m = A.shape[0]
idx = (np.arange(1,m+1) + (m+1)*np.arange(m-1)[:,None]).reshape(m,-1)
return A.ravel()[idx]
def skip_diag_strided(A):
m = A.shape[0]
strided = np.lib.stride_tricks.as_strided
s0,s1 = A.strides
return strided(A.ravel()[1:], shape=(m-1,m), strides=(s0+s1,s1)).reshape(m,-1)
时间-
In [528]: A = np.random.randint(11,99,(5000,5000))
In [529]: %timeit skip_diag_masking(A)
...: %timeit skip_diag_broadcasting(A)
...: %timeit skip_diag_strided(A)
...:
10 loops, best of 3: 56.1 ms per loop
10 loops, best of 3: 82.1 ms per loop
10 loops, best of 3: 32.6 ms per loop
这篇关于删除numpy数组的对角元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!