[英]Mode/Median/Mean of a 3d numpy array
I have a 3d numpy array and my goal is to get the mean/mode/median of it. 我有一个3d numpy数组,我的目标是得到它的均值/模式/中位数。
It has a shape of [500,300,3] 它的形状为[500,300,3]
And I would like to get for example: 我想举个例子:
[430,232,22] As the mode [430,232,22]作为模式
Is there a way to do this? 有没有办法做到这一点? The standard np.mean(array) gives me a very large array. 标准的np.mean(数组)给了我一个非常大的数组。
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 它给了我一个长度为3的1d np数组
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. 请注意, np.median
需要一个扁平数组,因此需要重新np.median
。 And bincount only handles 1D inputs, hence the list comprehension, coupled with a little transposition magic for unpacking. 而bincount只处理1D输入,因此列表理解,再加上一个用于解包的转换魔法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.