python矩阵删除行_从numpy矩阵中删除/提取子矩阵的最快方法

这篇博客探讨了如何快速地从numpy矩阵中删除指定行和列的问题。作者比较了几种不同的方法,包括使用np.delete函数两次以及利用高级索引和切片的方式。测试表明,尽管使用切片的方法代码较长,但其执行速度最快。
摘要由CSDN通过智能技术生成

I have a square matrix that is NxN (N is usually >500). It is constructed using a numpy array.

I need to extract a new matrix that has the i-th column and row removed from this matrix. The new matrix is (N-1)x(N-1).

I am currently using the following code to extract this matrix:

new_mat = np.delete(old_mat,idx_2_remove,0)

new_mat = np.delete(old_mat,idx_2_remove,1)

I have also tried to use:

row_indices = [i for i in range(0,idx_2_remove)]

row_indices += [i for i in range(idx_2_remove+1,N)]

col_indices = row_indices

rows = [i for i in row_indices for j in col_indices]

cols = [j for i in row_indices for j in col_indices]

old_mat[(rows, cols)].reshape(len(row_indices), len(col_indices))

But I found this is slower than using np.delete() in the former. The former is still quite slow for my application.

Is there a faster way to accomplish what I want?

Edit 1:

It seems the following is even faster than the above two, but not by much:

new_mat = old_mat[row_indices,:][:,col_indices]

解决方案

Here are 3 alternatives I quickly wrote:

Repeated delete:

def foo1(arr, i):

return np.delete(np.delete(arr, i, axis=0), i, axis=1)

Maximal use of slicing (may need some edge checks):

def foo2(arr,i):

N = arr.shape[0]

res = np.empty((N-1,N-1), arr.dtype)

res[:i, :i] = arr[:i, :i]

res[:i, i:] = arr[:i, i+1:]

res[i:, :i] = arr[i+1:, :i]

res[i:, i:] = arr[i+1:, i+1:]

return res

Advanced indexing:

def foo3(arr,i):

N = arr.shape[0]

idx = np.r_[:i,i+1:N]

return arr[np.ix_(idx, idx)]

Test that they work:

In [874]: x = np.arange(100).reshape(10,10)

In [875]: np.allclose(foo1(x,5),foo2(x,5))

Out[875]: True

In [876]: np.allclose(foo1(x,5),foo3(x,5))

Out[876]: True

Compare timings:

In [881]: timeit foo1(arr,100).shape

4.98 ms ± 190 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [882]: timeit foo2(arr,100).shape

526 µs ± 1.57 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [883]: timeit foo3(arr,100).shape

2.21 ms ± 112 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

So the slicing is fastest, even if the code is longer. It looks like np.delete works like foo3, but one dimension at a time.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值