3d numpy数组的模式/中位数/均值 [英] Mode/Median/Mean of a 3d numpy array

查看:85
本文介绍了3d numpy数组的模式/中位数/均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3d的numpy数组,我的目标是获取它的均值/众数/中位数.

I have a 3d numpy array and my goal is to get the mean/mode/median of it.

它的形状为[500,300,3]

It has a shape of [500,300,3]

我想举个例子:

[430,232,22]作为模式

[430,232,22] As the mode

有没有办法做到这一点?标准的np.mean(array)给了我很大的数组.

Is there a way to do this? The standard np.mean(array) gives me a very large array.

我不知道这是否正确?

weather_image.mean(axis=0).mean(axis=0)

它给我一个1d np数组,长度为3

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)

请注意,np.median需要平整的数组,因此需要重塑形状.而且bincount只处理一维输入,因此列表理解以及用于拆包的一些易变魔术.

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.

这篇关于3d numpy数组的模式/中位数/均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆