我有一些数据,我正试图用lmfit的模型来建模
具体来说,我在测量超导电阻。我试图将实验数据(电阻与温度)拟合到一个模型中,该模型包含临界温度Tc(材料相关)、低于Tc的电阻(名义上为0)和高于Tc的电阻(结构相关)
下面是我用来绘制数据的代码的简化版本(带有模拟数据),以及输出图
我没有得到任何错误,但正如您所看到的,我也没有得到与我的数据匹配的拟合
我做错了什么?这是我第一次使用lmfit和Model,所以我可能犯了一个新手错误。我认为我在跟踪the lmfit example,但正如我所说,我显然做错了什么
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from lmfit import Model
def main():
x = np.linspace(0, 12, 50)
x_ser = pd.Series(x) # Simulated temperature data
y1 = [0] * 20
y2 = [10] * 30
y1_ser = pd.Series(y1) # Simulated resistance data below Tc
y2_ser = pd.Series(y2) # Simulated resistance data above Tc (
y_ser = y1_ser.append(y2_ser, ignore_index=True)
xcrit_model = Model(data_equation)
params = xcrit_model.make_params(y1_guess=0, y2_guess=12, xcrit_guess=9)
print('params: {}'.format(params))
result = xcrit_model.fit(y_ser, params, x=x_ser)
print(result.fit_report())
plt.plot(x_ser, y_ser, 'bo', label='simulated data')
plt.plot(x_ser, result.init_fit, 'k.', label='initial fit')
plt.plot(x_ser, result.best_fit, 'r:', label='best fit')
plt.legend()
plt.show()
def data_equation(x, y1_guess, y2_guess, xcrit_guess):
x_lt_xcrit = x[x < xcrit_guess]
x_ge_xcrit = x[x >= xcrit_guess]
y1 = [y1_guess] * x_lt_xcrit.size
y1_ser = pd.Series(data=y1)
y2 = [y2_guess] * x_ge_xcrit.size
y2_ser = pd.Series(data=y2)
y = y1_ser.append(y2_ser, ignore_index=True)
return y
if __name__ == '__main__':
main()
lmfit
(基本上所有类似的解算器)使用连续变量,并通过对参数值进行微小更改来研究它们是如何改变结果的,并观察这对拟合的影响但是
xcrit_guess
参数仅用作离散变量。如果其值从9.0000更改为9.00001,则拟合根本不会更改所以,基本上,不要这样做:
相反,您应该使用更平滑的S形步长函数。事实上,
lmfit
有一个内置的。因此,您可以尝试类似的方法(请注意,将numpy.array转换为pandas.Series没有任何意义-代码只会将这些转换回numpy数组):相关问题 更多 >
编程相关推荐