Python 3d numpy阵列的模式/中值/平均值

Python 3d numpy阵列的模式/中值/平均值,python,numpy,scipy,Python,Numpy,Scipy,我有一个3d numpy阵列,我的目标是得到它的平均值/模式/中值 它的形状为[500300,3] 我想举个例子: [430232,22]作为模式 有办法做到这一点吗?标准的np.mean(数组)给了我一个非常大的数组 我不知道这是不是真的对 weather_image.mean(axis=0).mean(axis=0) 它给了我一个1d np数组,长度为3你想得到沿前两个轴的平均值/中值/模式。这应该起作用: data = np.random.randint(1000, size=(500,

我有一个3d numpy阵列,我的目标是得到它的平均值/模式/中值

它的形状为[500300,3]

我想举个例子:

[430232,22]作为模式

有办法做到这一点吗?标准的np.mean(数组)给了我一个非常大的数组

我不知道这是不是真的对

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

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

你想得到沿前两个轴的平均值/中值/模式。这应该起作用:

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)
data=np.random.randint(1000,大小=(500300,3))
>>>np.平均值(数据,轴=(0,1))#单位:nunpy>=1.7
阵列([499.06044499.01136498.60614667])
>>>np.平均值(np.平均值(数据,轴=0,轴=0)#单位:numpy<1.7
阵列([499.06044499.01136498.60614667])
>>>中位数(数据整形(-1,3),轴=0)
数组([499,499,498.])#模式
>>>np.argmax([np.bincount(x)表示数据中的x。重塑(-1,3.T],轴=1)
数组([240519842],dtype=int64)

请注意,
np.median
需要一个展平的数组,因此需要重塑。bincount只处理1D输入,因此列表理解,再加上解包时的一点换位魔法。

有什么想法,请告诉我现在发现的任何人,np.MIDAL不再需要扁平数组。自版本1.9.0以来,支持轴序列,默认操作是计算展平阵列的中值,除非轴参数中另有规定。