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

标签 python curve-fitting gaussian

我想用 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/

相关文章:

xna-4.0 - 试图使高斯模糊更强/更模糊 - XNA 4.0 HLSL

python - 更改目录下的文件夹名和文件名

python - 复制迭代器和生成组合

python - Xpath scrapy 结果不符合预期

python - 如何在 Python 中进行指数和对数曲线拟合?我发现只有多项式拟合

c++ - 在C++中使用高斯概率密度函数

Python LDAP 和 Active Directory 问题

python - Scipy 的 curve_fit 没有给出合理的结果

用于全局变量的洛伦兹拟合的 Matlab 函数

numpy - 如何通过Python : NumPy/SciPy?直接在频域中生成高斯分布的随机样本