Python 绘制一维高斯分布函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14873203/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:43:01  来源:igfitidea点击:

Plotting of 1-dimensional Gaussian distribution function

pythonplotgaussian

提问by pythonnewbie

How do I make plots of a 1-dimensional Gaussian distribution function using the mean and standard deviation parameter values (μ, σ) = (?1, 1), (0, 2), and (2, 3)?

如何使用均值和标准差参数值 (μ, σ) = (?1, 1)、(0, 2) 和 (2, 3) 绘制一维高斯分布函数图?

I'm new to programming, using Python.

我是编程新手,使用 Python。

Thank you in advance!

先感谢您!

采纳答案by danodonovan

With the excellent matplotliband numpypackages

与优秀matplotlibnumpy

from matplotlib import pyplot as mp
import numpy as np

def gaussian(x, mu, sig):
    return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))

x_values = np.linspace(-3, 3, 120)
for mu, sig in [(-1, 1), (0, 2), (2, 3)]:
    mp.plot(x_values, gaussian(x_values, mu, sig))

mp.show()

will produce something like plot showing one-dimensional gaussians produced by matplotlib

会产生类似的东西 显示由 matplotlib 生成的一维高斯曲线的图

回答by cvt1982

You are missing a parantheses in the denominator of your gaussian() function. As it is right now you divide by 2 and multiply with the variance (sig^2). But that is not true and as you can see of your plots the greater variance the more narrow the gaussian is - which is wrong, it should be opposit.

您在 gaussian() 函数的分母中缺少括号。就像现在一样,您除以 2 并乘以方差 (sig^2)。但事实并非如此,正如您在绘图中看到的那样,高斯分布越窄,方差越大 - 这是错误的,它应该相反。

So just change the gaussian() function to:

所以只需将 gaussian() 函数更改为:

def gaussian(x, mu, sig):
    return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))

回答by XValidated

you can read this tutorial for how to use functions of statistical distributions in python. http://docs.scipy.org/doc/scipy/reference/tutorial/stats.html

您可以阅读本教程,了解如何在 Python 中使用统计分布函数。http://docs.scipy.org/doc/scipy/reference/tutorial/stats.html

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np 

#initialize a normal distribution with frozen in mean=-1, std. dev.= 1
rv = norm(loc = -1., scale = 1.0)
rv1 = norm(loc = 0., scale = 2.0)
rv2 = norm(loc = 2., scale = 3.0)

x = np.arange(-10, 10, .1)

#plot the pdfs of these normal distributions 
plt.plot(x, rv.pdf(x), x, rv1.pdf(x), x, rv2.pdf(x))

回答by Felix

In addition to previous answers, I recommend to first calculate the ratio in the exponent, then taking the square:

除了之前的答案,我建议先计算指数中的比率,然后取平方:

def gaussian(x,x0,sigma):
  return np.exp(-np.power((x - x0)/sigma, 2.)/2.)

That way, you can also calculate the gaussian of very small or very large numbers:

这样,您还可以计算非常小或非常大的数字的高斯:

In: gaussian(1e-12,5e-12,3e-12)
Out: 0.64118038842995462

回答by ryanmartinneutrino

The correct form, based on the original syntax, and correctly normalized is:

基于原始语法并正确规范化的正确形式是:

def gaussian(x, mu, sig):
    return 1./(np.sqrt(2.*np.pi)*sig)*np.exp(-np.power((x - mu)/sig, 2.)/2)