沿 Python 中带有掩码的列取平均值
Taking mean along columns with masks in Python
我有一个包含一些测量数据的二维数组。仅考虑良好的数据,我必须在每一列中取平均值。
因此,我有另一个相同形状的二维数组,其中包含 1 和 0,显示 (i,j) 处的数据是好是坏。一些 "bad" 数据也可以是 nan。
def mean_exc_mask(x, mas): #x is the real data arrray
#mas tells if the data at the location is good/bad
sum_array = np.zeros(len(x[0]))
avg_array = np.zeros(len(x[0]))
items_array = np.zeros(len(x[0]))
for i in range(0, len(x[0])): #We take a specific column first
for j in range(0, len(x)): #And then parse across rows
if mas[j][i]==0: #If the data is good
sum_array[i]= sum_array[i] + x[j][i]
items_array[i]=items_array[i] + 1
if items_array[i]==0: # If none of the data is good for a particular column
avg_array[i] = np.nan
else:
avg_array[i] = float(sum_array[i])/items_array[i]
return avg_array
我得到的所有值都是 nan!
知道这里或其他地方出了什么问题吗?
该代码似乎对我有用,但您可以通过使用 Numpy 中的内置聚合来更简单地完成它:
(x*(m==0)).sum(axis=0)/(m==0).sum(axis=0)
我试过:
x=np.array([[-0.32220561, -0.93043128, 0.37695923],[ 0.08824206, -0.86961453, -0.54558324],[-0.40942331, -0.60216952, 0.17834533]])
和
m=array([[1, 1, 0],[1, 0, 0],[1, 1, 1]])
如果你post示例数据,通常更容易给出合格的答案。
我有一个包含一些测量数据的二维数组。仅考虑良好的数据,我必须在每一列中取平均值。 因此,我有另一个相同形状的二维数组,其中包含 1 和 0,显示 (i,j) 处的数据是好是坏。一些 "bad" 数据也可以是 nan。
def mean_exc_mask(x, mas): #x is the real data arrray
#mas tells if the data at the location is good/bad
sum_array = np.zeros(len(x[0]))
avg_array = np.zeros(len(x[0]))
items_array = np.zeros(len(x[0]))
for i in range(0, len(x[0])): #We take a specific column first
for j in range(0, len(x)): #And then parse across rows
if mas[j][i]==0: #If the data is good
sum_array[i]= sum_array[i] + x[j][i]
items_array[i]=items_array[i] + 1
if items_array[i]==0: # If none of the data is good for a particular column
avg_array[i] = np.nan
else:
avg_array[i] = float(sum_array[i])/items_array[i]
return avg_array
我得到的所有值都是 nan!
知道这里或其他地方出了什么问题吗?
该代码似乎对我有用,但您可以通过使用 Numpy 中的内置聚合来更简单地完成它:
(x*(m==0)).sum(axis=0)/(m==0).sum(axis=0)
我试过:
x=np.array([[-0.32220561, -0.93043128, 0.37695923],[ 0.08824206, -0.86961453, -0.54558324],[-0.40942331, -0.60216952, 0.17834533]])
和
m=array([[1, 1, 0],[1, 0, 0],[1, 1, 1]])
如果你post示例数据,通常更容易给出合格的答案。