numpy数组中每侧10行

numpy数组中每侧10行

本文介绍了取每个条目的最小值+-numpy数组中每侧10行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3d numpy数组,想要生成一个由每个值的最小值和正上方10行和正下方10行中的值组成的辅助数组(即,每个条目是21个值中的最小值),用于每个二维数组.

I have a 3d numpy array and want to generate a secondary array consisting of the minimum of each value and the values in the 10 rows directly above and 10 rows directly below (i.e each entry is the minimum value from 21 values) for each 2d array.

我一直在尝试使用'numpy.clip'处理数组的边缘-这里取最小值的值范围应该简单地减少到数组顶部/底部的值的10.大批.我想好像是'scipy.signal.argrelmin'之类的东西.

I've been trying to use 'numpy.clip' to deal with the edges of the array - here the range of values which the minimum is taken from should simply reduce to 10 at the values on the top/bottom of the array. I think something like 'scipy.signal.argrelmin' seems to be what I'm after.

到目前为止,这是我的代码,绝对不是解决问题的最佳方法:

Here's my code so far, definitely not the best way to go about it:

import numpy as np

array_3d = np.random.random_integers(50, 80, (3, 50, 18))
minimums = np.zeros(array_3d.shape)

for array_2d_index in range(len(array_3d)):
    for row_index in range(len(array_3d[array_2d_index])):
        for col_index in range(len(array_3d[array_2d_index][row_index])):
            minimums[array_2d_index][row_index][col_index] = min(array_3d[array_2d_index][np.clip(row_index-10, 0, 49):np.clip(row_index+10, 0, 49)][col_index])

我认为的主要问题是,这是从每个条目两侧的列而不是行中获取最小值,而行一直在产生索引错误.

The main issue I think is that this is taking the minimum from the columns either side of each entry instead of the rows, which has been giving index errors.

感谢您的任何建议,谢谢.

Would appreciate any advice, thanks.

推荐答案

方法1

这是使用 np.lib.stride_tricks.as_strided 的一种方法-

Here's one approach with np.lib.stride_tricks.as_strided -

def strided_3D_axis1(array_3d, L):
    s0,s1,s2 = array_3d.strides
    strided = np.lib.stride_tricks.as_strided
    m,n,r = array_3d.shape
    nL = n-L+1
    return strided(array_3d, (m,nL,L,r),(s0,s1,s1,s2))

out = strided_3D_axis1(array_3d, L=21).min(axis=-2)

样品运行-

1)输入:

In [179]: array_3d
Out[179]:
array([[[73, 65, 51, 76, 59],
        [74, 57, 75, 53, 70],
        [60, 74, 52, 54, 60],
        [54, 52, 62, 75, 50],
        [68, 56, 68, 63, 77]],

       [[62, 70, 60, 79, 74],
        [70, 68, 50, 74, 57],
        [63, 57, 69, 65, 54],
        [63, 63, 68, 58, 60],
        [70, 66, 65, 78, 78]]])

2)交叉视图:

In [180]: strided_3D_axis1(array_3d, L=3)
Out[180]:
array([[[[73, 65, 51, 76, 59],
         [74, 57, 75, 53, 70],
         [60, 74, 52, 54, 60]],

        [[74, 57, 75, 53, 70],
         [60, 74, 52, 54, 60],
         [54, 52, 62, 75, 50]],

        [[60, 74, 52, 54, 60],
         [54, 52, 62, 75, 50],
         [68, 56, 68, 63, 77]]],


       [[[62, 70, 60, 79, 74],
         [70, 68, 50, 74, 57],
         [63, 57, 69, 65, 54]],

        [[70, 68, 50, 74, 57],
         [63, 57, 69, 65, 54],
         [63, 63, 68, 58, 60]],

        [[63, 57, 69, 65, 54],
         [63, 63, 68, 58, 60],
         [70, 66, 65, 78, 78]]]])

3)基于min的条纹视图:

3) Strided view based min :

In [181]: strided_3D_axis1(array_3d, L=3).min(axis=-2)
Out[181]:
array([[[60, 57, 51, 53, 59],
        [54, 52, 52, 53, 50],
        [54, 52, 52, 54, 50]],

       [[62, 57, 50, 65, 54],
        [63, 57, 50, 58, 54],
        [63, 57, 65, 58, 54]]])

方法2

这里是另一个 broadcasting 在沿第二个轴创建所有滑动索引时-

Here's another with broadcasting upon creating all sliding indices along the second axis -

array_3d[:,np.arange(array_3d.shape[1]-L+1)[:,None] + range(L)].min(-2)

方法3

这里是另一个使用 Scipy's 1D minimum filter -

Here's another using Scipy's 1D minimum filter -

from scipy.ndimage.filters import minimum_filter1d as minf

L = 21
hL = (L-1)//2
out = minf(array_3d,L,axis=1)[:,hL:-hL]


运行时测试-

In [231]: array_3d = np.random.randint(50, 80, (3, 50, 18))

In [232]: %timeit strided_3D_axis1(array_3d, L=21).min(axis=-2)
10000 loops, best of 3: 54.2 µs per loop

In [233]: %timeit array_3d[:,np.arange(array_3d.shape[1]-L+1)[:,None] + range(L)].min(-2)
10000 loops, best of 3: 81.3 µs per loop

In [234]: L = 21
     ...: hL = (L-1)//2
     ...:

In [235]: %timeit minf(array_3d,L,axis=1)[:,hL:-hL]
10000 loops, best of 3: 32 µs per loop

这篇关于取每个条目的最小值+-numpy数组中每侧10行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:07