Python繪製正態分佈(高斯分佈)

參考資料

https://baike.baidu.com/item/%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83/829892?fr=aladdin

一維正態分佈

若隨機變量 XX 服從一個位置參數爲 μ\mu 、尺度參數爲 σ\sigma 的概率分佈,且其概率密度函數

f(x)=12πσexp((xμ)22σ2)f(x) ={1\over \sqrt {2\pi\sigma}} exp(-{(x-\mu)^2\over2\sigma^2})

則這個隨機變量就稱爲正態隨機變量,正態隨機變量服從的分佈就稱爲正態分佈,記作 XN(μ,σ2)X \sim N (\mu,\sigma^2) ,讀作 XX 服從 N(μ,σ2)N (\mu,\sigma^2) ,或 XX 服從正態分佈。

標準正態分佈
μ=0,σ=1\mu=0,\sigma=1 時,正態分佈就成爲標準正態分佈

Python 繪製正態分佈的代碼

import numpy as np
import matplotlib.pyplot as plt
import math
 
 
def normal_distribution(x, mu, sigma):
    return np.exp( -1 * ( (x-mu) ** 2) / ( 2 * (sigma ** 2)) ) / (math.sqrt( 2 * np.pi ) * sigma)

mu, sigma = 0, 1
x = np.linspace( mu - 6 * sigma, mu + 6 * sigma, 100)
y = normal_distribution(x, mu, sigma)
plt.plot(x, y, 'r', label='mu = 0,sigma = 1')
plt.legend()
plt.grid()
plt.show()

顯示效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章