Python模擬實(shí)現(xiàn)高斯分布擬合
什么是正態(tài)分布或高斯分布
當(dāng)我們繪制一個(gè)數(shù)據(jù)集(如直方圖)時(shí),圖表的形狀就是我們所說的分布。最常見的連續(xù)值形狀是鐘形曲線,也稱為高斯分布或正態(tài)分布。
它以德國數(shù)學(xué)家卡爾·弗里德里希·高斯的名字命名。遵循高斯分布的一些常見示例數(shù)據(jù)集是體溫、人的身高、汽車?yán)锍?、IQ分?jǐn)?shù)。
讓我們嘗試生成理想的正態(tài)分布,并使用Python繪制它。
如何在Python中繪制高斯分布
我們有像Numpy、scipy和matplotlib這樣的庫來幫助我們繪制理想的正態(tài)曲線。
import numpy as np import scipy as sp from scipy import stats import matplotlib.pyplot as plt ## generate the data and plot it for an ideal normal curve ## x-axis for the plot x_data = np.arange(-5, 5, 0.001) ## y-axis as the gaussian y_data = stats.norm.pdf(x_data, 0, 1) ## plot data plt.plot(x_data, y_data)
輸出

x軸上的點(diǎn)是觀測值,y軸是每個(gè)觀測值的似然性。
我們使用np.arange()在范圍(-5,5)內(nèi)生成規(guī)則間隔的觀測值。然后我們通過norm.pdf()函數(shù)運(yùn)行它,平均值為0.0,標(biāo)準(zhǔn)差為1,它返回該觀察的可能性。0附近的觀測值是最常見的,而-5.0和5.0附近的觀測值是罕見的。pdf()函數(shù)的技術(shù)術(shù)語是概率密度函數(shù)。
高斯函數(shù)
首先,讓我們將數(shù)據(jù)擬合到高斯函數(shù)。我們的目標(biāo)是找到最適合我們數(shù)據(jù)的A和B的值。首先,我們需要為高斯函數(shù)方程編寫一個(gè)Python函數(shù)。該函數(shù)應(yīng)該接受自變量(x值)和所有構(gòu)成它的參數(shù)。
#Define the Gaussian function def gauss(x, H, A, x0, sigma): return H + A * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))
我們將使用python模塊scipy.optimize中的函數(shù)curve_fit來擬合我們的數(shù)據(jù)。它使用非線性最小二乘法將數(shù)據(jù)擬合為函數(shù)形式。您可以通過使用Jupyter notebook或scipy在線文檔中的help函數(shù)了解有關(guān)curve_fit的更多信息。
curve_fit函數(shù)有三個(gè)必需的輸入:要擬合的函數(shù)、x數(shù)據(jù)和要擬合的y數(shù)據(jù)。有兩個(gè)輸出:第一個(gè)是參數(shù)的最優(yōu)值的數(shù)組,第二個(gè)是參數(shù)的估計(jì)協(xié)方差矩陣,您可以從中計(jì)算參數(shù)的標(biāo)準(zhǔn)誤差。
示例1:
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit xdata = [ -10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] ydata = [1.2, 4.2, 6.7, 8.3, 10.6, 11.7, 13.5, 14.5, 15.7, 16.1, 16.6, 16.0, 15.4, 14.4, 14.2, 12.7, 10.3, 8.6, 6.1, 3.9, 2.1] # Recast xdata and ydata into numpy arrays so we can use their handy features xdata = np.asarray(xdata) ydata = np.asarray(ydata) plt.plot(xdata, ydata, 'o') # Define the Gaussian function def Gauss(x, A, B): y = A*np.exp(-1*B*x**2) return y parameters, covariance = curve_fit(Gauss, xdata, ydata) fit_A = parameters[0] fit_B = parameters[1] fit_y = Gauss(xdata, fit_A, fit_B) plt.plot(xdata, ydata, 'o', label='data') plt.plot(xdata, fit_y, '-', label='fit') plt.legend()
輸出

示例2:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as mpl
# Let's create a function to model and create data
def func(x, a, x0, sigma):
return a*np.exp(-(x-x0)**2/(2*sigma**2))
# Generating clean data
x = np.linspace(0, 10, 100)
y = func(x, 1, 5, 2)
# Adding noise to the data
yn = y + 0.2 * np.random.normal(size=len(x))
# Plot out the current state of the data and model
fig = mpl.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, c='k', label='Function')
ax.scatter(x, yn)
# Executing curve_fit on noisy data
popt, pcov = curve_fit(func, x, yn)
#popt returns the best fit values for parameters of the given model (func)
print (popt)
ym = func(x, popt[0], popt[1], popt[2])
ax.plot(x, ym, c='r', label='Best fit')
ax.legend()
fig.savefig('model_fit.png')輸出

到此這篇關(guān)于Python模擬實(shí)現(xiàn)高斯分布擬合的文章就介紹到這了,更多相關(guān)Python高斯分布內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python進(jìn)度條神器tqdm使用實(shí)例詳解
Python進(jìn)度條神器tqdm是一個(gè)快速、可擴(kuò)展的進(jìn)度條工具,可以輕松地為Python腳本添加進(jìn)度條。它可以在循環(huán)中自動(dòng)計(jì)算進(jìn)度,并在終端中顯示進(jìn)度條,讓用戶了解程序的運(yùn)行情況。tqdm還支持多線程和多進(jìn)程,并且可以自定義進(jìn)度條的樣式和顯示方式。2023-06-06
Ubuntu18.04中Python2.7與Python3.6環(huán)境切換
這篇文章主要為大家詳細(xì)介紹了Ubuntu18.04中Python2.7與Python3.6環(huán)境切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python網(wǎng)絡(luò)爬蟲精解之正則表達(dá)式的使用說明
正則表達(dá)式是對字符串操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個(gè)“規(guī)則字符串”,這個(gè)“規(guī)則字符串”用來表達(dá)對字符串的一種過濾邏輯2021-09-09
Python高效實(shí)現(xiàn)Excel轉(zhuǎn)TXT文本
在數(shù)據(jù)處理場景中,將Excel文件轉(zhuǎn)換為純文本(TXT)格式便成為了一個(gè)常見的需求,本文將解析如何通過?Spire.XLS?for?Python?實(shí)現(xiàn)?Excel?到TXT?的高效轉(zhuǎn)換,有需要的可以了解下2026-01-01

