计算具有不同x值的y值的平均值

问题描述:

我尝试从不同的数组(例如np.mean(,axis=1),但具有不同的x值)计算y的平均值.

I tried to calculate the average of y from different arrays such as np.mean(,axis=1), but with different x values.

要生成xy数组,我使用了以下代码:

To produce x and y arrays, I used the code as below:

x1=np.arange(10)
x2 = np.arange(10)+1  
x3 = np.arange(10)+2
x4 = np.arange(10)+3
y1 = x1+1
y2 = x2+2
y3 = x3+3
y4 = x4 +4

代码产生:

x1 = [0 1 2 3 4 5 6 7 8 9]
x2 = [ 1  2  3  4  5  6  7  8  9 10]
x3 = [ 2  3  4  5  6  7  8  9 10 11]
x4 = [ 3  4  5  6  7  8  9 10 11 12]
y1 = [ 1  2  3  4  5  6  7  8  9 10]
y2 = [ 3  4  5  6  7  8  9 10 11 12]
y3 = [ 5  6  7  8  9 10 11 12 13 14]
y4 = [ 7  8  9 10 11 12 13 14 15 16]

如果我绘制(x1,y1),(x2,y2),(x3,y3),(x4,y4),则y值将从0到16之间的x值分布如下.在图中,某些x值只有一个y值,而其他x值可能具有多个值.我想取每个x值的y值的平均值.

If I plot (x1,y1), (x2,y2), (x3,y3), (x4,y4), y values are distributed from x values between 0 and 16 as below. Some x value have just one y values and other may have several values in the plot. I would like to take an average of y values at each x values.

import numpy_indexed as npi
x = np.concatenate([x1,x2,x3,x4])
y = np.concatenate([y1,y2,y3,y4])
x_unique, y_mean = npi.group_by(x).mean(y)