简体   繁体   English

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

[英]calculate average of y values with different x values

I tried to calculate the average of y from different arrays such as np.mean(,axis=1) , but with different x values. 我试图计算不同数组的y的平均值,例如np.mean(,axis=1) ,但是x值不同。

To produce x and y arrays, I used the code as below: 为了生成xy数组,我使用了如下代码:

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. 如果我绘制(x1,y1),(x2,y2),(x3,y3),(x4,y4),则y值从0到16之间的x值分布,如下所示。 Some x value have just one y values and other may have several values in the plot. 某些x值只有一个y值,而其他值可能在图中有多个值。 I would like to take an average of y values at each x values. 我想在每个x值处取y个平均值。

在此输入图像描述

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)

Here's an alternative pandas solution: 这是另一种pandas解决方案:

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
#..........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM