Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用numpy的步幅从给定子阵列中查找最大值_Python_Numpy_Matrix_Stride - Fatal编程技术网

Python 使用numpy的步幅从给定子阵列中查找最大值

Python 使用numpy的步幅从给定子阵列中查找最大值,python,numpy,matrix,stride,Python,Numpy,Matrix,Stride,我得到了这样的2D数组 我的任务是在不同颜色绘制的子数组中找到最大值。我必须使用跨步和as_跨步。到目前为止,我的代码如下所示: a=np.vstack(([0,1,2,3,4,5],[6,7,8,9,10,11],[12,13,14,15,16,17],[18,19,20,21,22,23])) print(np.max(np.lib.stride_tricks.as_strided(a,(2,3),strides=(24,4)))) 它正确地显示了第一个块的最大值,即8,但我不知道如何移动

我得到了这样的2D数组

我的任务是在不同颜色绘制的子数组中找到最大值。我必须使用跨步和as_跨步。到目前为止,我的代码如下所示:

a=np.vstack(([0,1,2,3,4,5],[6,7,8,9,10,11],[12,13,14,15,16,17],[18,19,20,21,22,23]))
print(np.max(np.lib.stride_tricks.as_strided(a,(2,3),strides=(24,4))))
它正确地显示了第一个块的最大值,即8,但我不知道如何移动到矩阵的其他部分。是否有任何方法可以移动到矩阵的其他部分,以便显示子数组的最大值

注意:这个任务是从我的python编程入门课开始的,所以不需要编写任何复杂的函数,我甚至会说这是不可取的

In [297]: a = np.arange(24).reshape(4,6)                                                       
In [298]: a                                                                                    
Out[298]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
如果我们将尺寸4调整为2,2,将尺寸6调整为2,3:

In [299]: a.reshape(2,2,2,3)                                                                   
Out[299]: 
array([[[[ 0,  1,  2],
         [ 3,  4,  5]],

        [[ 6,  7,  8],
         [ 9, 10, 11]]],


       [[[12, 13, 14],
         [15, 16, 17]],

        [[18, 19, 20],
         [21, 22, 23]]]])
块的顺序不正确,但我们可以通过转置来纠正:

In [300]: a.reshape(2,2,2,3).transpose(0,2,1,3)                                                
Out[300]: 
array([[[[ 0,  1,  2],
         [ 6,  7,  8]],

        [[ 3,  4,  5],
         [ 9, 10, 11]]],


       [[[12, 13, 14],
         [18, 19, 20]],

        [[15, 16, 17],
         [21, 22, 23]]]])
In [305]: a.reshape(2,2,2,3).transpose(0,2,1,3).strides                                        
Out[305]: (96, 24, 48, 8)
然后获取每个2d内部块的最大值:

In [301]: a.reshape(2,2,2,3).transpose(0,2,1,3).max((2,3))                                     
Out[301]: 
array([[ 8, 11],
       [20, 23]])
a、 重塑2,2,2,3。max1,3获得相同的max

好的,这不是用步幅完成的,但它给了我如何使用步幅的想法

a本身的步幅:

重塑后:

In [304]: a.reshape(2,2,2,3).strides                                                           
Out[304]: (96, 48, 24, 8)
转置后:

In [300]: a.reshape(2,2,2,3).transpose(0,2,1,3)                                                
Out[300]: 
array([[[[ 0,  1,  2],
         [ 6,  7,  8]],

        [[ 3,  4,  5],
         [ 9, 10, 11]]],


       [[[12, 13, 14],
         [18, 19, 20]],

        [[15, 16, 17],
         [21, 22, 23]]]])
In [305]: a.reshape(2,2,2,3).transpose(0,2,1,3).strides                                        
Out[305]: (96, 24, 48, 8)
因此,我们可以直接使用这些步骤:

In [313]: np.lib.stride_tricks.as_strided(a,(2,2,2,3),(96,24,48,8))                            
Out[313]: 
array([[[[ 0,  1,  2],
         [ 6,  7,  8]],

        [[ 3,  4,  5],
         [ 9, 10, 11]]],


       [[[12, 13, 14],
         [18, 19, 20]],

        [[15, 16, 17],
         [21, 22, 23]]]])
将numpy作为np导入 从numpy.lib.stride\u在跨步时导入技巧 矩阵=np.arange24.6 maxtrix=np.array[as_-striddematrix[0],2,3,matrix.strides.max, 如_-striddematrix[0][3:6],2,3,matrix.strides.max, 如_-striddematrix[2],2,3,matrix.strides.max, 如_-striddematrix[2][3:6],2,3,matrix.strides.max ].E2,2 如果你想要一个max的2,2数组,我想你想要一个2,2,2,3形状的跨步数组int:看看a.reshave2,2,2,3.strips。正确的步幅值是,只是顺序不正确。