A. python 怎麼求標准正態分布某個值
示例:
1、from numpy import *;
2、def rand_Matrix():
3、randArr=random.randn(2,3);
4、randMat=mat(randArr);
5、return randMat;
一種結果如下:
1、matrix([[ 0.3150869 , -0.02041996, -0.15361071],
2、[-0.75507988, 0.80393683, -0.31790917]])
(1)轉化正態分布python擴展閱讀
Python正態分布概率計算方法:
def st_norm(u):
'''標准正態分布'''
import math
x=abs(u)/math.sqrt(2)
T=(0.0705230784,0.0422820123,0.0092705272,
0.0001520143,0.0002765672,0.0000430638)
E=1-pow((1+sum([a*pow(x,(i+1))
for i,a in enumerate(T)])),-16)
p=0.5-0.5*E if u<0 else 0.5+0.5*E
return(p)
def norm(a,sigma,x):
'''一般正態分布'''
u=(x-a)/sigma
return(st_norm(u))
while 1:
'''輸入一個數時默認為標准正態分布
輸入三個數(空格隔開)時分別為期望、方差、x
輸入 stop 停止'''
S=input('please input the parameters: ')
if S=='stop':break
try:
L=[float(s) for s in S.split()]
except:
print('Input error!')
continue
if len(L)==1:
print('f(x)=%.5f'%st_norm(L[0]))
elif len(L)==3:
print('f(x)=%.5f'%norm(L[0],L[1],L[2]))
else:
print('Input error!')
B. 如何用python語言,產生某一區間內的正態分布的100個隨機數
import matplotlib.pyplot as plt
import scipy.stats as stats
lower, upper = 3.5, 6
mu, sigma = 5, 0.7
X = stats.truncnorm(
(lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma)
N = stats.norm(loc=mu, scale=sigma)
fig, ax = plt.subplots(2, sharex=True)
ax[0].hist(X.rvs(10000), normed=True)
ax[1].hist(N.rvs(10000), normed=True)
plt.show()
C. 如何將已知數據用python寫成正態分布並且畫圖
importnumpyasnp
importmatplotlib.pyplotasplt
y=[2,5,7,10,16,23,20,16,9,6,6,3,1,1]
x=[59,60,61,62,63,64,65,66,67,68,69,70,71,72]
fig,ax=plt.subplots()
ax.bar(x,y,0.3,alpha=0.5,color='b',label='abc')
plt.axis([55,75,0,25])
ax.set_xlabel('XXX')
ax.set_ylabel('YYY')
ax.set_title('ABC')
ax.legend()
fig.tight_layout()
plt.show()