[英]when plotting a pdf in python for a normal distribution and the standard deviation is 2, will the mean still be zero?
So based on my understanding of normal distribution the mean is zero by default when the standard deviation is 1. I was given an assignment to write a python program to generate a PDF of a normally distributed function with the range from 10 to 45 with a standard deviation of 2. Will the mean still be zero?因此,根据我对正态分布的理解,当标准差为 1 时,默认情况下均值为零。 2 的偏差。均值仍然为零吗? I tried this but my plot doesn't form a bell shape.我试过了,但我的情节没有形成钟形。 I don't know what I am doing wrong.我不知道我做错了什么。
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
mu=0 # mean
sigma=2
x=np.arange(10,45,0.1)
y=stats.norm.pdf(x, 0, sigma)
plt.plot(x,y)
plt.show()
Here since the range of random variables is between 10 to 45, so the mean will lie in between this range of values, around 27. You need to get the same using the mean function and then use your code as follows:这里因为随机变量的范围在 10 到 45 之间,所以平均值将位于这个值范围之间,大约为 27。您需要使用 mean 函数获得相同的值,然后使用您的代码如下:
y=stats.norm.pdf(x, x.mean(), sigma )
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10,45,0.1)
sigma = 2
print('Mean :', round(x.mean(), 2),'SD :', sigma)
plt.plot(x, norm.pdf(x,x.mean(),sigma), 'r1', lw=2, alpha=0.5, label='norm PDF')
plt.legend(loc='best')
plt.show()
Which prints:哪个打印:
Mean : 27.45 SD : 2
And shows the shape of the probability density function:并显示了概率密度函数的形状:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.