python - 用子矩阵替换 numpy 矩阵元素

标签 python performance numpy indexing vectorization

鉴于我有一个索引方阵,例如:

idxs = np.array([[1, 1],
                 [0, 1]])

和彼此大小相同的方阵数组(不一定与idxs大小相同):

mats = array([[[ 0. ,  0. ],
               [ 0. ,  0.5]],

              [[ 1. ,  0.3],
               [ 1. ,  1. ]]])

我想用mats中对应的矩阵替换idxs中的每个索引,得到:

array([[ 1. ,  0.3,  1. ,  0.3],
       [ 1. ,  1. ,  1. ,  1. ],
       [ 0. ,  0. ,  1. ,  0.3],
       [ 0. ,  0.5,  1. ,  1. ]])

mats[idxs] 给我一个嵌套版本:

array([[[[ 1. ,  0.3],
         [ 1. ,  1. ]],

        [[ 1. ,  0.3],
         [ 1. ,  1. ]]],


       [[[ 0. ,  0. ],
         [ 0. ,  0.5]],

        [[ 1. ,  0.3],
         [ 1. ,  1. ]]]])

所以我尝试使用 reshape,但是没有用! mats[idxs].reshape(4,4) 返回:

array([[ 1. ,  0.3,  1. ,  1. ],
       [ 1. ,  0.3,  1. ,  1. ],
       [ 0. ,  0. ,  0. ,  0.5],
       [ 1. ,  0.3,  1. ,  1. ]])

如果有帮助,我发现 skimage.util.view_as_blocks 与我需要的完全相反(它可以将我想要的结果转换为嵌套的 mats[idxs] 形式)。

有没有(希望非常)快速的方法来做到这一点?对于应用程序,我的 mats 仍然只有几个小矩阵,但我的 idxs 将是一个高达 2^15 阶的方阵,在这种情况下我'将替换超过一百万个索引以创建一个新的 2^16 阶矩阵。

非常感谢您的帮助!

最佳答案

我们正在使用这些索引对输入数组的第一个轴进行索引。要获得 2D 输出,我们只需要置换轴并在之后 reshape 。因此,一种方法是使用 np.transpose/np.swapaxesnp.reshape , 像这样 -

mats[idxs].swapaxes(1,2).reshape(-1,mats.shape[-1]*idxs.shape[-1])

sample 运行-

In [83]: mats
Out[83]: 
array([[[1, 1],
        [7, 1]],

       [[6, 6],
        [5, 8]],

       [[7, 1],
        [6, 0]],

       [[2, 7],
        [0, 4]]])

In [84]: idxs
Out[84]: 
array([[2, 3],
       [0, 3],
       [1, 2]])

In [85]: mats[idxs].swapaxes(1,2).reshape(-1,mats.shape[-1]*idxs.shape[-1])
Out[85]: 
array([[7, 1, 2, 7],
       [6, 0, 0, 4],
       [1, 1, 2, 7],
       [7, 1, 0, 4],
       [6, 6, 7, 1],
       [5, 8, 6, 0]])

使用 np.take 提升性能对于重复索引

对于重复索引,为了提高性能,我们最好使用 np.take 沿 axis=0 进行索引。让我们列出这两种方法,并用具有许多重复索引的 idxs 计时。

函数定义-

def simply_indexing_based(mats, idxs):
    ncols = mats.shape[-1]*idxs.shape[-1]
    return mats[idxs].swapaxes(1,2).reshape(-1,ncols)

def take_based(mats, idxs):np.take(mats,idxs,axis=0)
    ncols = mats.shape[-1]*idxs.shape[-1]
    return np.take(mats,idxs,axis=0).swapaxes(1,2).reshape(-1,ncols)

运行时测试-

In [156]: mats = np.random.randint(0,9,(10,2,2))

In [157]: idxs = np.random.randint(0,10,(1000,1000))
                 # This ensures many repeated indices

In [158]: out1 = simply_indexing_based(mats, idxs)

In [159]: out2 = take_based(mats, idxs)

In [160]: np.allclose(out1, out2)
Out[160]: True

In [161]: %timeit simply_indexing_based(mats, idxs)
10 loops, best of 3: 41.2 ms per loop

In [162]: %timeit take_based(mats, idxs)
10 loops, best of 3: 27.3 ms per loop

因此,我们看到了 1.5x+ 的整体改进。

只是为了了解 np.take 的改进,让我们单独为索引部分计时 -

In [168]: %timeit mats[idxs]
10 loops, best of 3: 22.8 ms per loop

In [169]: %timeit np.take(mats,idxs,axis=0)
100 loops, best of 3: 8.88 ms per loop

对于这些数据大小,它是 2.5x+。不错!

关于python - 用子矩阵替换 numpy 矩阵元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305324/

相关文章:

使用 Inno Setup 的 Python 分发模块

javascript - Angular 1.x 迁移性能问题

iphone - 如何使用 GLshorts 表示法线坐标或纹理坐标?

python - Opencv:旋转矩形感兴趣区域外的零像素?

python - numpy 循环和 where 条件的替代方法

python - 即使正确安装 pip 和 mysql 连接器后,我也无法在 Pycharm 上连接到 MySQL

Python下载一个完整的网页(包括CSS)

linux - 为什么我的 linux 熵有这么低的上限?

python - 导入函数参数 numpy

c++ - 从 Python 调用 C++ opencv 函数(将 cv::Mat 发送到使用 opencv 的 C++ dll)