使用 python 进行非线性回归 - 有什么简单的方法可以更好地拟合这些数据?

2024-01-18

我有一些想要拟合的数据,这样我就可以对给定特定温度的物理参数的值进行一些估计。

我使用 numpy.polyfit 作为二次模型,但拟合效果并不像我希望的那么好,而且我在回归方面没有太多经验。

我已经包含了散点图和 numpy 提供的模型:S 与温度;蓝点是实验数据,黑线是模型 https://i.stack.imgur.com/PaCO3.png

x 轴是温度(以 C 为单位),y 轴是参数,我们将其称为 S。这是实验数据,但理论上 S 应随着温度升高而趋向于 0,随着温度降低而趋于 1。

我的问题是:如何更好地拟合这些数据?我应该使用哪些库,什么样的函数可以比多项式更好地逼近这些数据,等等?

如果有帮助的话,我可以提供代码、多项式系数等。

这是我的数据的 Dropbox 链接。 https://www.dropbox.com/s/w8n98fzjnp19uef/8cb_datapoints_T_vs_S.csv?dl=0(为了避免混淆,需要注意一点,虽然它不会改变实际的回归,但该数据集中的温度列是 Tc - T,其中 Tc 是转变温度 (40C)。我使用 pandas 通过计算 40 将其转换为 T - X)。


此示例代码使用具有两个形状参数 a 和 b 以及一个偏移项(不影响曲率)的方程。方程为“y = 1.0 / (1.0 + exp(-a(x-b))) + Offset”,参数值 a = 2.1540318329369712E-01,b = -6.6744890642157646E+00,Offset = -3.5241299859669645E-01,其中得出 R 平方为 0.988,RMSE 为 0.0085。

该示例包含您发布的数据,其中包含用于拟合和绘图的 Python 代码,并使用 scipy.optimize. Differential_evolution 遗传算法进行自动初始参数估计。差分进化的 scipy 实现使用拉丁超立方算法来确保对参数空间的彻底搜索,这需要搜索范围 - 在此示例代码中,这些范围基于最大和最小数据值。

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

xData = numpy.array([19.1647, 18.0189, 16.9550, 15.7683, 14.7044, 13.6269, 12.6040, 11.4309, 10.2987, 9.23465, 8.18440, 7.89789, 7.62498, 7.36571, 7.01106, 6.71094, 6.46548, 6.27436, 6.16543, 6.05569, 5.91904, 5.78247, 5.53661, 4.85425, 4.29468, 3.74888, 3.16206, 2.58882, 1.93371, 1.52426, 1.14211, 0.719035, 0.377708, 0.0226971, -0.223181, -0.537231, -0.878491, -1.27484, -1.45266, -1.57583, -1.61717])
yData = numpy.array([0.644557, 0.641059, 0.637555, 0.634059, 0.634135, 0.631825, 0.631899, 0.627209, 0.622516, 0.617818, 0.616103, 0.613736, 0.610175, 0.606613, 0.605445, 0.603676, 0.604887, 0.600127, 0.604909, 0.588207, 0.581056, 0.576292, 0.566761, 0.555472, 0.545367, 0.538842, 0.529336, 0.518635, 0.506747, 0.499018, 0.491885, 0.484754, 0.475230, 0.464514, 0.454387, 0.444861, 0.437128, 0.415076, 0.401363, 0.390034, 0.378698])


def func(x, a, b, Offset): # Sigmoid A With Offset from zunzun.com
    return  1.0 / (1.0 + numpy.exp(-a * (x-b))) + Offset


# function for genetic algorithm to minimize (sum of squared error)
def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
    val = func(xData, *parameterTuple)
    return numpy.sum((yData - val) ** 2.0)


def generate_Initial_Parameters():
    # min and max used for bounds
    maxX = max(xData)
    minX = min(xData)
    maxY = max(yData)
    minY = min(yData)

    parameterBounds = []
    parameterBounds.append([minX, maxX]) # search bounds for a
    parameterBounds.append([minX, maxX]) # search bounds for b
    parameterBounds.append([0.0, maxY]) # search bounds for Offset

    # "seed" the numpy random number generator for repeatable results
    result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
    return result.x

# generate initial parameter values
geneticParameters = generate_Initial_Parameters()

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

print('Parameters', fittedParameters)

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('RMSE:', RMSE)
print('R-squared:', Rsquared)



##########################################################
# 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 python 进行非线性回归 - 有什么简单的方法可以更好地拟合这些数据? 的相关文章

随机推荐