使用python在范围内使用高斯函数生成数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16471763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating numbers with Gaussian function in a range using python
提问by Lily
I want to use the gaussian function in python to generate some numbers between a specific range giving the mean and variance
我想在 python 中使用高斯函数在特定范围之间生成一些数字,给出均值和方差
so lets say I have a range between 0 and 10
所以可以说我的范围在 0 到 10 之间
and I want my mean to be 3 and variance to be 4
我希望我的平均值为 3,方差为 4
mean = 3, variance = 4
均值 = 3,方差 = 4
how can I do that ?
我怎样才能做到这一点 ?
回答by Dan Lecocq
Use random.gauss. From the docs:
使用random.gauss. 从文档:
random.gauss(mu, sigma)
Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly
faster than the normalvariate() function defined below.
It seems to me that you can clamp the results of this, but that wouldn't make it a Gaussian distribution. I don't think you can satisfy all the constraints simultaneously. If you want to clamp it to the range [0, 10], you could get your numbers:
在我看来,你可以限制这个结果,但这不会使它成为高斯分布。我认为您无法同时满足所有约束。如果你想把它限制在 range [0, 10],你可以得到你的数字:
num = min(10, max(0, random.gauss(3, 4)))
But then the resulting distribution of numbers won't be truly Gaussian. In this case, it seems you can't have your cake and eat it, too.
但是,由此产生的数字分布不会是真正的高斯分布。在这种情况下,您似乎也不能吃蛋糕。
回答by Mark Ransom
If you have a small range of integers, you can create a list with a gaussian distribution of the numbers within that range and then make a random choice from it.
如果您有一个小范围的整数,您可以创建一个列表,该列表具有该范围内数字的高斯分布,然后从中随机选择。
回答by JimmyLamothe
There's probably a better way to do this, but this is the function I ended up creating to solve this problem:
可能有更好的方法来做到这一点,但这是我最终为解决这个问题而创建的函数:
import random
def trunc_gauss(mu, sigma, bottom, top):
a = random.gauss(mu,sigma))
while (bottom <= a <= top) == False:
a = random.gauss(mu,sigma))
return a
If we break it down line by line:
如果我们逐行分解:
import random
This allows us to use functions from the random library, which includes a gaussian random number generator (random.gauss).
这允许我们使用随机库中的函数,其中包括一个高斯随机数生成器 (random.gauss)。
def trunc_gauss(mu, sigma, bottom, top):
The function arguments allow us to specify the mean (mu) and variance (sigma), as well as the top and bottom of our desired range.
函数参数允许我们指定均值 (mu) 和方差 (sigma),以及所需范围的顶部和底部。
a = random.gauss(mu,sigma))
Inside the function, we generate an initial random number according to a gaussian distribution.
在函数内部,我们根据高斯分布生成初始随机数。
while (bottom <= a <= top) == False:
a = random.gauss(mu,sigma))
Next, the while loop checks if the number is within our specified range, and generates a new random number as long as the current number is outside our range.
接下来,while 循环检查数字是否在我们指定的范围内,只要当前数字不在我们的范围内,就会生成一个新的随机数。
return a
As soon as the number is inside our range, the while loop stops running and the function returns the number.
只要数字在我们的范围内,while 循环就会停止运行并且函数返回数字。
This should give a better approximation of a gaussian distribution, since we don't artificially inflate the top and bottom boundaries of our range by rounding up or down the outliers.
这应该可以更好地近似高斯分布,因为我们不会通过向上或向下舍入异常值来人为地扩大我们范围的顶部和底部边界。
I'm quite new to Python, so there are most probably simpler ways, but this worked for me.
我对 Python 很陌生,所以很可能有更简单的方法,但这对我有用。
回答by Danjoe
I was working on some numerical analytical computation and I ran into this python tutorial site - http://www.python-course.eu/weighted_choice_and_sample.php
我正在做一些数值分析计算,我遇到了这个 python 教程站点 - http://www.python-course.eu/weighted_choice_and_sample.php
Now, this is what I proffer as a solution should anyone be too busy as to not hit the site. I don't know how many gaussian values you need so I'll go with 100 as n, mu you gave as 3 and variance as 4 which makes sigma = 2. Here's the code:
现在,如果有人太忙而无法访问该站点,这就是我提供的解决方案。我不知道您需要多少个高斯值,所以我将 100 作为 n,mu 您给出的值为 3,方差为 4,这使得 sigma = 2。这是代码:
from random import gauss
n = 100
values = []
frequencies = {}
while len(values) < n:
value = gauss(3, 2)
if 0 < value < 10:
frequencies[int(value)] = frequencies.get(int(value), 0) + 1
values.append(value)
print(values)
I hope this helps. You can get the plot as well. It's all in the tutorials.
我希望这有帮助。你也可以得到情节。这都在教程中。
回答by Wojciech Moszczyński
You can use minimalistic code for 150 variables:
您可以对 150 个变量使用简约代码:
import numpy as np
s = np.random.normal(3,4,150) #<= mean = 3, variance = 4
print(s)
Everybody know normal distribution is another like random, stochastic distribution. So, we can check it by:
每个人都知道正态分布是另一种类似随机的随机分布。因此,我们可以通过以下方式进行检查:
import seaborn as sns
import matplotlib.pyplot as plt
AA1_plot = sns.distplot(s, kde=True, rug=False)
plt.show()

