[英]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.