gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:43:46 25 4
gpt4 key购买 nike

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

为了生成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

代码产生:

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 值处的 y 值的平均值。

enter image description here

最佳答案

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)

关于python - 计算具有不同 x 值的 y 值的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com