我想使用lmfit模块将函数拟合到可变数量的数据集,包括一些共享和一些单独的参数.
以下是生成高斯数据并分别拟合每个数据集的示例:
import numpy as np
import matplotlib.pyplot as plt
from lmfit import minimize, Parameters, report_fit
def func_gauss(params, x, data=[]):
A = params['A'].value
mu = params['mu'].value
sigma = params['sigma'].value
model = A*np.exp(-(x-mu)**2/(2.*sigma**2))
if data == []:
return model
return data-model
x = np.linspace( -1, 2, 100 )
data = []
for i in np.arange(5):
params = Parameters()
params.add( 'A' , value=np.random.rand() )
params.add( 'mu' , value=np.random.rand()+0.1 )
params.add( 'sigma', value=0.2+np.random.rand()*0.1 )
data.append(func_gauss(params,x))
plt.figure()
for y in data:
fit_params …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用lmfit (链接到docs)构建模型,我似乎无法找出为什么我ValueError: The input contains nan values在尝试适应模型时不断获得.
from lmfit import minimize, Minimizer, Parameters, Parameter, report_fit, Model
import numpy as np
def cde(t, Qi, at, vw, R, rhob_cb, al, d, r):
# t (time), is the independent variable
return Qi / (8 * np.pi * ((at * vw)/R) * t * rhob_cb * (np.sqrt(np.pi * ((al * vw)/R * t)))) * \
np.exp(- (R * (d - (t * vw)/ R)**2) / (4 * (al * vw) * …Run Code Online (Sandbox Code Playgroud) 我想使用lmfit 将椭圆测量数据拟合到复杂模型中.两个测量参数,psi和delta是复杂函数中的变量rho.
我可以尝试用共享参数或picewise方法将问题分离到实部和虚部,但是有没有办法直接用复杂函数来做?仅适合函数的实部工作,但是当我定义复杂的残差函数时,我得到:
TypeError:没有为复数定义排序关系.
下面是我的实际功能拟合代码和我尝试解决复杂的拟合问题:
from __future__ import division
from __future__ import print_function
import numpy as np
from pylab import *
from lmfit import minimize, Parameters, Parameter, report_errors
#=================================================================
# MODEL
def r01_p(eps2, th):
c=cos(th)
s=(sin(th))**2
stev= sqrt(eps2) * c - sqrt(1-(s / eps2))
imen= sqrt(eps2) * c + sqrt(1-(s / eps2))
return stev/imen
def r01_s(eps2, th):
c=cos(th)
s=(sin(th))**2
stev= c - sqrt(eps2) * sqrt(1-(s/eps2))
imen= c + …Run Code Online (Sandbox Code Playgroud) 我有实验数据:
xdata = [85,86,87,88,89,90,91,91.75,93,96,100,101,102,103,104,105,106,107.25,108.25,109,109.75,111,112,112.75,114,115.25,116,116.75,118,119.25,120,121,122,122.5,123.5,125.25,126,126.75,127.75,129.25,130.25,131,131.75,133,134.25,135,136,137,138,139,140,141,142,143,144,144.75,146,146.75,148,149.25,150,150.5,152,153.25,154,155,156.75,158,159,159.75,161,162,162.5,164,165,166]
ydata = [0.2,0.21,0.18,0.21,0.19,0.2,0.21,0.2,0.18,0.204,0.208,0.2,0.21,0.25,0.2,0.19,0.216,0.22,0.224,0.26,0.229,0.237,0.22,0.246,0.25,0.264,0.29,0.274,0.29,0.3,0.27,0.32,0.38,0.348,0.372,0.398,0.35,0.42,0.444,0.48,0.496,0.55,0.51,0.54,0.57,0.51,0.605,0.57,0.65,0.642,0.6,0.66,0.7,0.688,0.69,0.705,0.67,0.717,0.69,0.728,0.75,0.736,0.73,0.744,0.72,0.76,0.752,0.74,0.76,0.7546,0.77,0.74,0.758,0.74,0.78,0.76]
Run Code Online (Sandbox Code Playgroud)
和公式f(x) = m1 + m2 / (1 + e ^ (-m3*(x - m4))).我需要找到m1,
m2, m3, m4最小二乘法,其中0.05 <m1 <0.3 0.3 <m2 <0.8 0.05 <m3 <0.5 100 <m4 <200.
我用curve_fit,我的功能是:
def f(xdata, m1, m2, m3, m4):
if m1 > 0.05 and m1 < 0.3 and \
m2 > 0.3 and m2 < 0.8 and \
m3 > 0.05 and m3 < 0.5 and \
m4 > 100 and m4 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将此函数拟合到一些数据中:
但是当我使用我的代码时
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def f(x, start, end):
res = np.empty_like(x)
res[x < start] =-1
res[x > end] = 1
linear = np.all([[start <= x], [x <= end]], axis=0)[0]
res[linear] = np.linspace(-1., 1., num=np.sum(linear))
return res
if __name__ == '__main__':
xdata = np.linspace(0., 1000., 1000)
ydata = -np.ones(1000)
ydata[500:1000] = 1.
ydata = ydata + np.random.normal(0., 0.25, len(ydata))
popt, pcov = curve_fit(f, xdata, ydata, p0=[495., 505.])
print(popt, pcov)
plt.figure()
plt.plot(xdata, …Run Code Online (Sandbox Code Playgroud) I am trying to deconvolve complex gas chromatogram signals into individual gaussian signals. Here is an example, where the dotted line represents the signal I am trying to deconvolve.
I was able to write the code to do this using scipy.optimize.curve_fit; however, once applied to real data the results were unreliable. I believe being able to set bounds to my parameters will improve my results, so I am attempting to use lmfit, which allows this. I am having a problem …
我正在 Python 2.7 中运行拟合,并lmfit使用一些测试数据和以下代码。我需要重量为 的加权拟合1/y(使用 Leven-Marq. 例程)。我已经定义了权重并在这里使用它们:
from __future__ import division
from numpy import array, var
from lmfit import Model
from lmfit.models import GaussianModel, LinearModel
import matplotlib.pyplot as plt
import seaborn as sns
xd = array([1267, 1268, 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276,
1277, 1278, 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298, 1299, 1300,
1301, 1302, 1303, 1304, 1305, 1306, 1307, …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序来拟合一些拉曼光谱峰值。\n我需要返回拟合的参数(位置、幅度、HWHM)。
\n\n我使用 modul lmfit 创建一个带有约束的洛伦兹峰。
\n\n根据我的图形,拟合峰值和原始数据之间有很好的一致性。\n但是当拟合后提取参数时,我遇到了一个问题,程序仅返回初始值。
\n\n我绑定了“report_fit 模块”并更改了初始参数,但没有成功。参数值不会变化。
\n\n令我困扰的是,这个程序在我同事的电脑上运行,但在我的电脑上运行不了。\n所以问题可能出在我的 python 版本上。
\n\n我正在使用spyder 2.3.9,并在Windows 10下使用anaconda安装了python 3.4。\nlmfit模块0.9.3似乎可以部分工作,因为我可以获得一个很好的拟合协议(从图plt.plot中)。\n但是拟合后无法返回参数值。
\n\n这是我的代码:
\n\n#!/usr/bin/python3\n# -*- coding:utf-8 -*-\n\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom math import factorial\nfrom scipy.interpolate import interp1d\nfrom lmfit import minimize, Parameters #,report_fit\n\n##############################################################################\n# Fit of Raman peaks\n\ndef fmin150(pars,x,y): \n amp= pars[\'Amp_150\'].value\n cent=pars[\'Cent_150\'].value\n hwhm=pars[\'Wid_150\'].value\n a=pars[\'a_150\'].value\n b=pars[\'b_150\'].value\n peak = (amp*hwhm)/(((x-cent)**2)+(hwhm**2)) + ((a*x)+b)\n return peak - y \n\ndef fmin220(pars,x,y): \n amp= pars[\'Amp_220\'].value\n cent=pars[\'Cent_220\'].value\n hwhm=pars[\'Wid_220\'].value\n a=pars[\'a_220\'].value\n b=pars[\'b_220\'].value\n peak = …Run Code Online (Sandbox Code Playgroud) 如您所知,python 中的 lmfit 模块可以方便地扩展 scipy.optimize 函数的功能。
然而,我没有发现在我看来必要的东西:选择步长的可能性(用于偏导、参数空间中 chi2 的计算等......)。我曾经在 IDL 下安装时玩过这些步骤,我很惊讶我在 python 下没有找到这个。
很明显,默认的步长非常小,在拟合粗略模型时可能会导致恒定的 chi2……因此很尴尬。
所以我的问题是:在 python 下拟合时如何选择步骤?
这是关于从lmfit fit_report()(1)对象提取拟合统计信息的问题
在此 lmfit示例中,返回以下部分输出:
[[Model]]
Model(gaussian)
[[Fit Statistics]]
# function evals = 31
# data points = 101
# variables = 3
chi-square = 3.409
reduced chi-square = 0.035
Akaike info crit = -336.264
Bayesian info crit = -328.418
.
.
.
.
.
.
Run Code Online (Sandbox Code Playgroud)
我试图提取该Fit Statistics部分中的所有数量作为单独的变量。
for key in fit.params:
print(key, "=", fit.params[key].value, "+/-", fit.params[key].stderr)
Run Code Online (Sandbox Code Playgroud)
但是,这仅给出了模型参数。它没有提供拟合统计参数,这也很有用。我似乎在文档中找不到此内容。
有没有类似的方式来提取拟合统计参数(chi-square,reduced chi-square, …
我正在尝试使该数据适合高斯
x = [4170.177259096838, 4170.377258006199, 4170.577256915561, 4170.777255824922, 4170.977254734283, 4171.177253643645, 4171.377252553006, 4171.577251462368, 4171.777250371729, 4171.977249281091, 4172.177248190453, 4172.377247099814, 4172.577246009175, 4172.777244918537, 4172.977243827898, 4173.17724273726, 4173.377241646621, 4173.577240555983, 4173.777239465344, 4173.977238374706, 4174.177237284067, 4174.377236193429, 4174.57723510279, 4174.777234012152, 4174.977232921513, 4175.177231830875, 4175.377230740236, 4175.577229649598, 4175.777228558959, 4175.977227468321, 4176.177226377682, 4176.377225287044, 4176.577224196405, 4176.777223105767, 4176.977222015128, 4177.17722092449, 4177.377219833851, 4177.577218743213, 4177.777217652574, 4177.977216561936, 4178.177215471297, 4178.377214380659, 4178.57721329002, 4178.777212199382, 4178.977211108743, 4179.177210018105, 4179.377208927466, 4179.577207836828, 4179.777206746189, 4179.977205655551, 4180.177204564912, 4180.377203474274, 4180.577202383635, 4180.777201292997, 4180.977200202357, 4181.17719911172, 4181.377198021081, 4181.577196930443, 4181.777195839804, 4181.977194749166, 4182.177193658527, 4182.377192567888, 4182.5771914772495, 4182.777190386612, 4182.9771892959725, 4183.177188205335, 4183.377187114696, 4183.577186024058, 4183.777184933419, 4183.9771838427805, 4184.177182752143, 4184.3771816615035, 4184.5771805708655, 4184.777179480228, 4184.977178389589, 4185.1771772989505, 4185.3771762083115, …Run Code Online (Sandbox Code Playgroud) 我正在使用 lmfit 将我的数据拟合为高斯分布。我试图完成三件事:1)了解如何在 lmfit 中计算误差 2)如何在 lmfit 中包含我自己计算的误差 3)如何在拟合中绘制误差
def gaussian(x, amp, cen, fwhm):
return + amp * np.exp(-(x - cen) ** 2 / (2 * (fwhm / 2.35482) ** 2))
def gaussian_fit(x,y,guess=[1,0,0,5],varies=[True,True,True,True]):
c = 299792458 #m/s
gmod = Model(gaussian)
gmod.nan_policy = 'omit'
#x,y - your dataset to fit, with x and y values
print (np.max(y))
gmod.set_param_hint('amp', value=guess[0],vary=varies[0])
gmod.set_param_hint('cen', value=guess[1],vary=varies[1])
gmod.set_param_hint('fwhm', value=guess[2],vary=varies[2])
gmod.make_params()
result = gmod.fit(y,x=x,amp=guess[0], cen=guess[1], fwhm=guess[2])
amp = result.best_values['amp']
cen = result.best_values['cen']
fwhm = result.best_values['fwhm']
#level = result.best_values['level'] …Run Code Online (Sandbox Code Playgroud) lmfit ×12
python ×11
scipy ×3
data-fitting ×2
parameters ×2
gaussian ×1
modeling ×1
python-2.7 ×1
windows ×1