I currently have a numpy array of the shape (71, 136, 136, 130); the exact dimensions vary. This is volume data (e.g., temperature) in the form [Time, Z, Y, X]. I am allowing the user to specify the time range to collapse the Z, Y, X volume element-wise for the maximum, minimum, or mean.
I was able to successfully collapse the array element-wise for maximum and minimum in a Time index range of 20-70 (example indices) by using a while loop taking one Time slice at a time. However, this loop approach won't work when calculating the mean.
I should probably be able to perform these operations (maximum, minimum, or mean) without a loop structure. The np.maximum() and np.minimum() functions require two arrays, so I can't seem to arrange the code like np.maximum(array[20:70,:,:,:])
which I'd want Python/numpy to infer I only want the indices 20-70 used for the returned maximum array. The np.mean() function seems to operate a little differently, but in principle I'd like it to work the same way; averaging the data element-wise over only the time indices specified. Any suggestions would be appreciated. Thanks.
Based on comments here is some simplified example code. Note that when I get the data it is in the form of the full_array; I don't actually manually construct it.
import numpy as np
time0_xy_data = np.array([[22, 80, 17, 98, 42, 46], [36, 11, 95, 23, 77, 12]])
time1_xy_data = np.array([[12, 97, 85, 68, 44, 43], [15, 84, 91, 33, 13, 56]])
time2_xy_data = np.array([[45, 26, 37, 93, 2, 28], [73, 78, 72, 48, 62, 62]])
time3_xy_data = np.array([[42, 67, 59, 60, 55, 67], [21, 32, 88, 87, 23, 15]])
full_array = np.array([time0_xy_data, time1_xy_data, time2_xy_data, time3_xy_data])
# Get the element-wise maximum of [0:1,:,:,:]
maximum_slice = np.maximum(full_array[0:1 :, :])
print(maximum_slice)
# What I expect to get back:
maximum_slice = np.array([[22, 97, 85, 98, 44, 46], [36, 84, 95, 33, 77, 56]])
Use the axis
parameter of np.max
, np.min
, and np.mean
:
> np.max(full_data[0:2], axis=0)
array([[22, 97, 85, 98, 44, 46],
[36, 84, 95, 33, 77, 56]])
> np.mean(full_data[1:3], axis=0)
array([[28.5, 61.5, 61. , 80.5, 23. , 35.5],
[44. , 81. , 81.5, 40.5, 37.5, 59. ]])
> np.min(full_data[2:], axis=0)
array([[42, 26, 37, 60, 2, 28],
[21, 32, 72, 48, 23, 15]])
axis : None or int or tuple of ints, optional Axis or axes along which to operate. By default, flattened input is used.