Python模擬實現(xiàn)高斯分布擬合
什么是正態(tài)分布或高斯分布
當我們繪制一個數(shù)據(jù)集(如直方圖)時,圖表的形狀就是我們所說的分布。最常見的連續(xù)值形狀是鐘形曲線,也稱為高斯分布或正態(tài)分布。
它以德國數(shù)學家卡爾·弗里德里希·高斯的名字命名。遵循高斯分布的一些常見示例數(shù)據(jù)集是體溫、人的身高、汽車里程、IQ分數(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軸上的點是觀測值,y軸是每個觀測值的似然性。
我們使用np.arange()在范圍(-5,5)內(nèi)生成規(guī)則間隔的觀測值。然后我們通過norm.pdf()函數(shù)運行它,平均值為0.0,標準差為1,它返回該觀察的可能性。0附近的觀測值是最常見的,而-5.0和5.0附近的觀測值是罕見的。pdf()函數(shù)的技術術語是概率密度函數(shù)。
高斯函數(shù)
首先,讓我們將數(shù)據(jù)擬合到高斯函數(shù)。我們的目標是找到最適合我們數(shù)據(jù)的A和B的值。首先,我們需要為高斯函數(shù)方程編寫一個Python函數(shù)。該函數(shù)應該接受自變量(x值)和所有構成它的參數(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ù)了解有關curve_fit的更多信息。
curve_fit函數(shù)有三個必需的輸入:要擬合的函數(shù)、x數(shù)據(jù)和要擬合的y數(shù)據(jù)。有兩個輸出:第一個是參數(shù)的最優(yōu)值的數(shù)組,第二個是參數(shù)的估計協(xié)方差矩陣,您可以從中計算參數(shù)的標準誤差。
示例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')輸出

到此這篇關于Python模擬實現(xiàn)高斯分布擬合的文章就介紹到這了,更多相關Python高斯分布內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python基于文本內(nèi)容實現(xiàn)隱私信息提取與評估
這篇文章主要為大家介紹了Python如何實現(xiàn)基于文本內(nèi)容的用戶隱私泄露風險評估系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-03-03
使用Python和FastAPI實現(xiàn)MinIO斷點續(xù)傳功能
在分布式存儲和大數(shù)據(jù)應用中,斷點續(xù)傳是一個重要的功能,它允許大文件上傳在中斷后可以從中斷點恢復,而不是重新上傳整個文件,本文將介紹如何使用Python封裝MinIO的斷點續(xù)傳方法,需要的朋友可以參考下2024-12-12
用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實例
下面小編就為大家分享一篇用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨想過來看看吧2017-12-12

