gpt4 book ai didi

python - 估计曲线与高斯分布的相似度(在 Python 中)

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:28 25 4
gpt4 key购买 nike

我想用 Python 量化测量值曲线与高斯分布的相似度。

给出了两个值数组:

H=(0,5,10,15,20,25,30,35,40,50,70) 是以米为单位的高度

C(H)=(0,1,1,2,4,6,7,5,3,1,0)是测量的数量(例如浓度)

Python 有没有办法

a) 将高斯曲线拟合到 C(H) 的值?

b) 获得某种相似系数来描述曲线与高斯曲线的相似程度?

提前致谢

最佳答案

因为您特别要求 Python 代码,所以这里有一个图形 Python 曲线拟合器,使用您的数据并拟合高斯峰值方程。 RMSE 和 R 平方值应该是相似性的有用度量,因为它们一起描述了数据的高斯拟合质量。

plot

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

H=(0,5,10,15,20,25,30,35,40,50,70)
C=(0,1,1,2,4,6,7,5,3,1,0)

xData = numpy.array(H, dtype=float)
yData = numpy.array(C, dtype=float)


def func(x, a, b, c): # Gaussian peak
return a * numpy.exp(-0.5 * numpy.power((x-b) / c, 2.0))


# estimate initial parameters from the data
a_est = max(C)
b_est = (max(H) + min(H)) / 2
c_est = max(C)
initialParameters = numpy.array([a_est, b_est, c_est], dtype=float)

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters)

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)

# first the raw data as a scatter plot
axes.plot(xData, yData, 'D')

# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = func(xModel, *fittedParameters)

# now the model as a line plot
axes.plot(xModel, yModel)

axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label

plt.show()
plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

关于python - 估计曲线与高斯分布的相似度(在 Python 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59563317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com