gpt4 book ai didi

python : Generate normal distribution in the order of the bell

转载 作者:行者123 更新时间:2023-11-28 17:05:58 24 4
gpt4 key购买 nike

我想按钟声顺序生成正态分布。我使用此代码生成数字:

import numpy as np

mu,sigma,n = 0.,1.,1000

def normal(x,mu,sigma):
return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )

x = np.random.normal(mu,sigma,n) #generate random list of points from normal distribution
y = normal(x,mu,sigma) #evaluate the probability density at each point
x,y = x[np.argsort(y)],np.sort(y) #sort according to the probability density

这是建议的代码:Generating normal distribution in order python, numpy

但数字并不遵循钟形。有任何想法吗?非常感谢

最佳答案

有几件事让您感到困惑。

random.normal 从钟形曲线中随机绘制 n 个数字

所以您有 1000 个数字,每个数字都不同,都是从曲线中绘制的。要重新创建曲线,您需要应用一些分箱。每个 bin 中的点数将重新创建曲线(仅一个点本身很难代表概率)。在只有 1000 个点的 x 向量上使用一些广泛的分箱:

h,hx=np.histogram(x,bins=50)

并绘制 h 作为 hx 的函数(所以我将你的千位数字分为 50 个 bin,y 轴将显示数量垃圾箱中的点数: enter image description here

现在我们可以看到 x 是从钟形分布中抽取的 - 落入中心 bin 的机会由高斯分布决定。这是一个采样,因此每个点当然可能会有所不同 - 您使用的点越多,分箱越精细,效果就越好(更平滑)。

y = normal(x,mu,sigma)

这只是在任何给定的 x 上评估高斯分布,所以实际上,为 normal 提供围绕您的均值 (mu) 的任何数字列表,它将计算钟形曲线恰好(确切的概率)。绘制你的 yx (你的 x 本身是高斯并不重要,但它是平均值附近的 1000 点,所以它可以重新创建功能): enter image description here

看看它有多流畅?那是因为它不是采样,而是函数的精确计算。您可以使用 0 附近的任何 1000 个点,它看起来同样好。

关于 python : Generate normal distribution in the order of the bell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51012793/

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