简体   繁体   English

从 numpy 数组的每个子数组中查找最大值

[英]Finding maximum values from each subarrays of a numpy array

I have an array:我有一个数组:

x = array([[[ 0,  1,  2],
        [ 6,  7,  8],
        [12, 13, 14]],

       [[ 3,  4,  5],
        [ 9, 10, 11],
        [15, 16, 17]],

       [[18, 19, 20],
        [24, 25, 26],
        [30, 31, 32]],

       [[21, 22, 23],
        [27, 28, 29],
        [33, 34, 35]]])

I want to find the max values of each subarrays and store them in lets say an array.我想找到每个子数组的最大值并将它们存储在一个数组中。 So the output should be:所以 output 应该是:

output = array([14,17,32,35])

Now, one can easily do this using a loop, however, I want to avoid it.现在,可以使用循环轻松地做到这一点,但是,我想避免它。 np.max(x) is giving output 35, that is the max value of the entire array. np.max(x)给出 output 35,即整个数组的最大值。 np.max(axis) also is not working (I am not very sure it would work, but I tried anyway) Anyone, can you help? np.max(axis)也不起作用(我不太确定它会起作用,但我还是尝试了)任何人,你能帮忙吗?

Simply:简单地:

[max(j) for j in [max(i) for i in x.tolist()]]

Output: Output:

[14, 17, 32, 35] [14、17、32、35]

You can use np.max for axis=1 twice您可以将np.max用于axis=1两次

x.max(axis=1).max(axis=1)   

OUTPUT OUTPUT

Out[203]: array([14, 17, 32, 35])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 numpy 的步幅从给定的子数组中查找最大值 - Finding max values from given subarrays using numpy's strides 查找(并记录)numpy数组的切片的最大值 - Finding (and recording) the maximum values of slices of a numpy array 找到最大值及其索引以移动numpy ndarray的子​​数组的Python方法是什么? - What is a pythonic way of finding maximum values and their indices for moving subarrays for numpy ndarray? 在 NumPy 数组中查找最小值和最大值的索引 - Finding the index of the minimum and maximum values in a NumPy array 如何找到 Numpy 数组的 M 个元素的 N 个最大乘积子数组? - How to find N maximum product subarrays of M elements of a Numpy array? 如何从arrays的numpy数组中获取每个数组的N个最大值 - How to get N maximum values of each array from a numpy array of arrays Numpy:通过分级查找关联的最小值和最大值 - Numpy: Finding minimum and maximum values from associations through binning 在numpy数组中重新排列子数组? - Rearrange subarrays in a numpy array? 交换numpy数组的子数组 - Swap subarrays of a numpy array 根据另一个数组的值(未排序,但分组)将 NumPy 数组拆分为子数组 - Split a NumPy array into subarrays according to the values (not sorted, but grouped) of another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM