I have a 3d numpy array and my goal is to get the mean/mode/median of it.
It has a shape of [500,300,3]
And I would like to get for example:
[430,232,22] As the mode
Is there a way to do this? The standard np.mean(array) gives me a very large array.
I don't know if this is actually right?
weather_image.mean(axis=0).mean(axis=0)
It gives me a 1d np array with a length of 3
You want to get the mean/median/mode along the first two axes. This should work:
data = np.random.randint(1000, size=(500, 300, 3))
>>> np.mean(data, axis=(0, 1)) # in nunpy >= 1.7
array([ 499.06044 , 499.01136 , 498.60614667])
>>> np.mean(np.mean(data, axis=0), axis=0) # in numpy < 1.7
array([ 499.06044 , 499.01136 , 498.60614667])
>>> np.median(data.reshape(-1, 3), axis=0)
array([ 499., 499., 498.]) # mode
>>> np.argmax([np.bincount(x) for x in data.reshape(-1, 3).T], axis=1)
array([240, 519, 842], dtype=int64)
Note that np.median
requires a flattened array, hence the reshape. And bincount only handles 1D inputs, hence the list comprehension, coupled with a little transposition magic for unpacking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With