计算不同长度的数组的均值 [英] Calculating Mean of arrays with different lengths

查看:72
本文介绍了计算不同长度的数组的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当多个数组的长度不同时,是否可以计算多个数组的均值?我正在使用numpy.假设我有:

Is it possible to calculate the mean of multiple arrays, when they may have different lengths? I am using numpy. So let's say I have:

numpy.array([[1, 2, 3, 4, 8],    [3, 4, 5, 6, 0]])
numpy.array([[5, 6, 7, 8, 7, 8], [7, 8, 9, 10, 11, 12]])
numpy.array([[1, 2, 3, 4],       [5, 6, 7, 8]])

现在,我想计算均值,但忽略缺失"的元素(自然,我不能只添加零,因为这会弄乱均值)

Now I want to calculate the mean, but ignoring elements that are 'missing' (Naturally, I can not just append zeros as this would mess up the mean)

有没有一种方法可以在不迭代数组的情况下进行操作?

Is there a way to do this without iterating through the arrays?

PS.这些数组都是二维的,但对于该数组将始终具有相同数量的坐标. IE.第一个数组是5和5,第二个数组是6和6,第三个数组是4和4.

PS. These arrays are all 2-D, but will always have the same amount of coordinates for that array. I.e. the 1st array is 5 and 5, 2nd is 6 and 6, 3rd is 4 and 4.

一个例子:

np.array([[1, 2],    [3, 4]])
np.array([[1, 2, 3], [3, 4, 5]])
np.array([[7],       [8]])

这必须给

(1+1+7)/3  (2+2)/2   3/1
(3+3+8)/3  (4+4)/2   5/1

并以图形方式:

[1, 2]    [1, 2, 3]    [7]
[3, 4]    [3, 4, 5]    [8]

现在想象一下,这些二维数组彼此重叠放置,坐标重叠导致该坐标的均值.

Now imagine that these 2-D arrays are placed on top of each other with coordinates overlapping contributing to that coordinate's mean.

推荐答案

numpy.ma.mean allows you to compute the mean of non-masked array elements. However, to use numpy.ma.mean, you have to first combine your three numpy arrays into one masked array:

import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[1, 2, 3], [3, 4, 5]])
z = np.array([[7], [8]])

arr = np.ma.empty((2,3,3))
arr.mask = True
arr[:x.shape[0],:x.shape[1],0] = x
arr[:y.shape[0],:y.shape[1],1] = y
arr[:z.shape[0],:z.shape[1],2] = z
print(arr.mean(axis = 2))

收益

[[3.0 2.0 3.0]
 [4.66666666667 4.0 5.0]]

这篇关于计算不同长度的数组的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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