问题
I tried to calculate the average of y
from different arrays such as np.mean(,axis=1)
, but with different x
values.
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
The code produces:
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]
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.
回答1:
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)
回答2:
Here's an alternative pandas
solution:
import pandas as pd
pd.concat([pd.Series(y1,index=x1),
pd.Series(y2,index=x2),
pd.Series(y3,index=x3),
pd.Series(y4,index=x4)], axis=1).mean(axis=1)
#0 1.0
#1 2.5
#2 4.0
#3 5.5
#..........
来源:https://stackoverflow.com/questions/41821539/calculate-average-of-y-values-with-different-x-values