[英]fitting data to a custom model in python
Hi So I am fairly used to python but this is the first time I am using python for data analysis and I was wondering if you could shed some light on an issue I am having. 嗨,我很熟悉python,但这是我第一次使用python进行数据分析,我想知道您是否可以对我遇到的问题有所了解。
I need to fit about 4000 different plots to the following function : b+e*A*(1.19104*(10**-16))*((x*(10**-9))**-5)*((-1+np.exp(0.0143878/(T*x*(10**-9))))**-1)
in this function I want to restrict b, e and A to certain values for each plot and the value of the variable T to shift according to the data. 我需要将大约4000个不同的图拟合到以下函数:
b+e*A*(1.19104*(10**-16))*((x*(10**-9))**-5)*((-1+np.exp(0.0143878/(T*x*(10**-9))))**-1)
在此函数中,我想将b,e和A限制为每个图和变量T的值根据数据移动。
I tried using scipy optimize but I couldn't figure out how to hold parameters with that one. 我尝试使用scipy优化,但无法弄清楚如何使用该参数保存参数。
I tried using pyAstronomy funcfit but for one it is extremely inefficient (or maybe my code is idk) and I wasn't getting data that looked what I had approximated the data should look like. 我尝试使用pyAstronomy funcfit,但它效率极低(或者我的代码是idk),而且我没有得到看起来像我估计的数据应该看起来像的数据。
Finally I am currently trying to use lmfit but my code here seems to have all of the parameters stay the same as the guess and not vary at all. 最后,我目前正在尝试使用lmfit,但是这里的代码似乎所有参数都与猜测相同,并且完全没有差异。 I am confused on how to proceed.
我对如何进行感到困惑。 my current code looks like this...
我当前的代码看起来像这样...
def bbnm(x,b,e,T,A):
y = b+e*A*(1.19104*(10**-16))*((x*(10**-9))**-5)*((-1+np.exp(0.0143878/(T*x*(10**-9))))**-1)
return y
params = bbmodel.make_params(b=0,e=.99,T=5100,A=wgeometry)
params['b'].vary = False
params['e'].vary = False
params['A'].vary = False
params['T'].vary = True
bb = bbmodel.fit(power[1465:2510],params,x=wavelength[1465:2510])
Here is code to restrict scipy's curve_fit parameters to within specified bounds. 这是将scipy的curve_fit参数限制在指定范围内的代码。 In this example, the first parameter's bounds are +/- infinity (unbounded), the second parameter's bounds are +/- 100 but the fitted parameter is within the bounds and fitted normally, and the third parameter is restricted by its bounds.
在此示例中,第一个参数的界限为+/-无穷大(无界),第二个参数的界限为+/- 100,但fitted参数在界限之内且正常拟合,而第三个参数受其界限限制。
import numpy
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
xData = numpy.array([5.0, 6.1, 7.2, 8.3, 9.4])
yData = numpy.array([ 10.0, 18.4, 20.8, 23.2, 35.0])
def standardFunc(data, a, b, c):
return a * data + b * data**2 + c
# some initial parameter values - must be within bounds
initialParameters = numpy.array([1.0, 1.0, 1.0])
# bounds on parameters - initial parameters must be within these
lowerBounds = (-numpy.Inf, -100.0, -5.0)
upperBounds = (numpy.Inf, 100.0, 5.0)
parameterBounds = [lowerBounds, upperBounds]
fittedParameters, pcov = curve_fit(standardFunc, xData, yData, initialParameters, bounds = parameterBounds)
# values for display of fitted function
a, b, c = fittedParameters
# for plotting the fitting results
xPlotData = numpy.linspace(min(xData), max(xData), 50)
y_plot = standardFunc(xPlotData, a, b, c)
plt.plot(xData, yData, 'D') # plot the raw data as a scatterplot
plt.plot(xPlotData, y_plot) # plot the equation using the fitted parameters
plt.show()
print('fitted parameters:', fittedParameters)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.