将数据拟合到python中的自定义模型 [英] fitting data to a custom model in python

查看:83
本文介绍了将数据拟合到python中的自定义模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我相当熟悉python,但这是我第一次使用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.

我需要为以下功能拟合约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 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.

我尝试使用scipy优化,但无法弄清楚如何使用该参数保存参数.

I tried using scipy optimize but I couldn't figure out how to hold parameters with that one.

我尝试使用pyAstronomy funcfit,但是它效率极低(或者我的代码是idk),而且我没有得到看起来像我应该近似的数据的数据.

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.

最后,我目前正在尝试使用lmfit,但是我的代码似乎使所有参数都与猜测保持相同,并且完全没有变化.我对如何进行感到困惑.我当前的代码如下:

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. 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])

推荐答案

此处是将scipy的curve_fit参数限制在指定范围内的代码.在此示例中,第一个参数的界限为+/-无穷大(无界),第二个参数的界限为+/- 100,但拟合的参数在界限之内并且可以正常拟合,而第三个参数受其界限限制.

Here is code to restrict scipy's curve_fit parameters to within specified bounds. 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.

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)

这篇关于将数据拟合到python中的自定义模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆