Python 使用lmfit绘制和建模数据-Fit不';数据不匹配。我做错了什么?

Python 使用lmfit绘制和建模数据-Fit不';数据不匹配。我做错了什么?,python,lmfit,Python,Lmfit,我有一些数据,我正试图用lmfit的模型来建模 具体来说,我在测量超导电阻。我试图将实验数据(电阻与温度)拟合到一个模型中,该模型包含临界温度Tc(材料相关)、低于Tc的电阻(名义上为0)和高于Tc的电阻(结构相关) 下面是我用来绘制数据的代码的简化版本(带有模拟数据),以及输出图 我没有得到任何错误,但正如您所看到的,我也没有得到与我的数据匹配的拟合 我做错了什么?这是我第一次使用lmfit和Model,所以我可能犯了一个新手错误。我认为我在跟踪,但正如我所说,我显然做错了什么 import

我有一些数据,我正试图用lmfit的模型来建模

具体来说,我在测量超导电阻。我试图将实验数据(电阻与温度)拟合到一个模型中,该模型包含临界温度Tc(材料相关)、低于Tc的电阻(名义上为0)和高于Tc的电阻(结构相关)

下面是我用来绘制数据的代码的简化版本(带有模拟数据),以及输出图

我没有得到任何错误,但正如您所看到的,我也没有得到与我的数据匹配的拟合

我做错了什么?这是我第一次使用lmfit和Model,所以我可能犯了一个新手错误。我认为我在跟踪,但正如我所说,我显然做错了什么

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()

将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
从lmfit导入模型
def main():
x=np.linspace(0,12,50)
x_ser=pd.系列(x)#模拟温度数据
y1=[0]*20
y2=[10]*30
y1_ser=pd系列(y1)#低于Tc的模拟电阻数据
y2_ser=pd.系列(y2)#高于Tc的模拟电阻数据(
y_ser=y1_ser.append(y2_ser,ignore_index=True)
xcrit_模型=模型(数据_方程)
参数=xcrit_模型。生成参数(y1_猜测=0,y2_猜测=12,xcrit_猜测=9)
打印('params:{}'。格式(params))
结果=xcrit_model.fit(y_ser,params,x=x_ser)
打印(result.fit_report())
plt.plot(x_ser,y_ser,'bo',label='simulateddata')
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数据方程(x,y1,y2,xcrit):
x_lt_xcrit=x[x=xcrit\u guess]
y1=[y1_猜测]*x_lt_xcrit.size
y1_ser=pd.系列(数据=y1)
y2=[y2\u猜测]*x\u ge\u xcrit.size
y2_ser=pd.系列(数据=y2)
y=y1序列追加(y2序列,忽略索引=True)
返回y
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
lmfit
(以及基本上所有类似的解算器)处理连续变量,并通过对参数值进行微小更改来研究它们如何改变结果,并查看这对拟合的影响

但是您的
xcrit\u guess
参数仅用作离散变量。如果其值从9.0000更改为9.00001,则拟合将完全不变

所以,基本上,不要这样做:

x_lt_xcrit = x[x < xcrit_guess]
x_ge_xcrit = x[x >= xcrit_guess]

感谢您提供的信息和建议!和re:Series我怀疑可能是这样的,但数据是作为pandas系列提供给我的,因此编写分析脚本来使用Series是最简单的。@BobInBaltimore使用pandas.Series很好,
lmfit
会将它们转换为numpy数组,如果您不这样做的话。@M Newville-再次感谢您的帮助。我已将您的答案标记为已接受的答案。我还查看了lmfit StepModel文档。我尝试了各种模型形式。默认的“线性”模型在我的“result=xcrit_model.fit(…)”步骤中抛出了一个ValueError,而“atan”模型似乎在执行“反向”操作“适合-我期望低时高,我期望高时低。我稍后会尝试找出这些,主要是为了我自己的教育。“物流”模型给出的结果与你建议的“erf”模型大致相同。我将坚持使用“erf”
import numpy as np
from lmfit.models import StepModel
import matplotlib.pyplot as plt

x = np.linspace(0, 12, 50)
y = 9.5*np.ones(len(x))
y[:26] = 0.0
y = y + np.random.normal(size=len(y), scale=0.0002)

xcrit_model = StepModel(form='erf')
params = xcrit_model.make_params(amplitude=4, center=5, sigma=1)

result = xcrit_model.fit(y, params, x=x)
print(result.fit_report())

plt.plot(x, y, 'bo', label='simulated data')
plt.plot(x, result.init_fit, 'k', label='initial fit')
plt.plot(x, result.best_fit, 'r:', label='best fit')
plt.legend()
plt.show()