gpt4 book ai didi

python - 将散点数据转换为误差条等于标准差的分箱数据

转载 作者:太空狗 更新时间:2023-10-30 00:30:07 25 4
gpt4 key购买 nike

我有一堆分散在 x, y 上的数据。如果我想根据 x 对它们进行分箱,并在它们上放置等于标准差的误差条,我该怎么做?

我在 python 中唯一知道的是遍历 x 中的数据并根据 bins (max(X)-min(X)/nbins) 对它们进行分组,然后遍历这些 block 以找到标准。我确信有更快的方法可以使用 numpy 执行此操作。

我希望它看起来类似于“vert symmetric”:http://matplotlib.org/examples/pylab_examples/errorbar_demo.html

最佳答案

您可以使用 np.histogram 对数据进行分箱。我正在重用 this other answer 中的代码计算分箱 y 的均值和标准差:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)
y = np.sin(2*np.pi*x) + 2 * x * (np.random.rand(100)-0.5)
nbins = 10

n, _ = np.histogram(x, bins=nbins)
sy, _ = np.histogram(x, bins=nbins, weights=y)
sy2, _ = np.histogram(x, bins=nbins, weights=y*y)
mean = sy / n
std = np.sqrt(sy2/n - mean*mean)

plt.plot(x, y, 'bo')
plt.errorbar((_[1:] + _[:-1])/2, mean, yerr=std, fmt='r-')
plt.show()

enter image description here

关于python - 将散点数据转换为误差条等于标准差的分箱数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556930/

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