使用 lmfit 評(píng)估模型
我想在輸入變量的定義范圍內(nèi)繪制計(jì)算并繪制模型的預(yù)測(cè),我正在嘗試使用lmfit. 這是我正在處理的代碼。該模型被定義為只有一個(gè)自變量t的函數(shù),但函數(shù)本身也使用另一組獨(dú)立的觀察值。我可以計(jì)算評(píng)估模型的 new_times,但不能對(duì)另一組觀察值做同樣的事情。除了糟糕的配合(這里本身不是問(wèn)題)之外,我還強(qiáng)調(diào)了我在使用時(shí)遇到的錯(cuò)誤,lmfit因?yàn)槲艺J(rèn)為它有效:import numpy as npimport matplotlib.pyplot as pltfrom lmfit import Modelimport scipy.integrate as itimport scipy.constants as sccdef new_f_function(t, sum, f0, a, b, c, T0): obs_f = f0 + it.cumtrapz(-a * p**c + b, t-T0, initial=0) new_f = obs_f*(1+sum/scc.c) return new_f# Create Modelmodel = Model(new_f_function, independent_vars=['t'])# Initialize Parameterparams = model.make_params()params['sum'].value = 1.483 params['sum'].min = 1.47params['sum'].max = 1.50params['f0'].value = 1.483 params['f0'].min = 1.47params['f0'].max = 1.50params['a'].value = 1.483 params['a'].min = 1.47params['a'].max = 1.50params['b'].value = 1.483 params['c'].value = 1.483 params['T0'].value = 1.483 result = model.fit(y_obs, params, weights=(1./y_obs_err), t=times, scale_covar=False)print result.fit_report()# New x-values to evaluate the model x_fit = np.linspace(min(times)-10., max(times)+10, 1e4) fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(3, 6), sharex='all')ax1.scatter(x=times, y=p, marker='+', c='k')ax2.errorbar(x=times, y=y_obs, yerr=y_obs_err, marker='.', ls='', label='DATA')ax2.plot(times, result.best_fit, label='best fit')new_predictions = result.eval(**result.best_values)#ax2.plot(x_fit, new_predictions, label='extended fit') # This gives error: `ValueError: x and y must have same first dimension, but have shapes (10000,) and (45,)`predicted = result.eval(t=x_fit) # This gives error: `raise ValueError("If given, length of x along axis must be the "ValueError: If given, length of x along axis must be the same as y.`plt.legend()plt.show()我在這里想念什么?
查看完整描述