【问题标题】:With using lmfit module in python, how to call the parameters in the model?在python中使用lmfit模块,如何调用模型中的参数?
【发布时间】:2015-10-11 03:58:45
【问题描述】:
import numpy as np

import matplotlib.pyplot as plt

from lmfit import minimize, Parameters, Parameter, report_fit


# create data to be fitted


x = np.linspace(0, 15, 301)

data = (5. * np.sin(2 * x - 0.1) * np.exp(-x*x*0.025) +

np.random.normal(size=len(x), scale=0.2) )

# define objective function: returns the array to be minimized


def fcn2min(params, x, data):

    """ model decaying sine wave, subtract data"""
    amp = params['amp'].value
    shift = params['shift'].value
    omega = params['omega'].value
    decay = params['decay'].value
    model = amp * np.sin(x * omega + shift) * np.exp(-x*x*decay)


    return model - data


# create a set of Parameters

params = Parameters()

params.add('amp',   value= 10,  min=0)

params.add('decay', value= 0.1)

params.add('shift', value= 0.0, min=-np.pi/2., max=np.pi/2)

params.add('omega', value= 5.0)


# do fit, here with leastsq model

result = minimize(fcn2min, params, args=(x, data))


# calculate final result

final = data + result.residual


# try to plot results
plt.plot(x,data,'k+')

plt.plot(x,final,'r')

plt.show()

在这段代码中,我想在python中调用'amp','shift'等参数。 打印(amp)..各种东西 拟合后如何在python中调用这些参数? 当我使用 print(amp) 时,会显示错误消息;名称“amp”未定义。如何使用打印功能打印这些拟合参数? (等打印(放大器))

【问题讨论】:

  • 很难理解您真正想要或要求什么?您的代码中哪些有效,哪些无效?
  • 如果你运行代码,你会发现代码运行良好。但是,我想将拟合参数称为“amp”和“decay”。当我使用 print(amp) 时,会显示错误消息;名称“amp”未定义。如何使用打印功能打印这些参数? (等打印(放大器))

标签: python numpy matplotlib lmfit


【解决方案1】:

您可能试图在函数之外打印该数据。 ampshiftomegadecay 变量位于 fc2min 的本地范围内,因此只能在函数内部访问。你的数据分析技能似乎远远超过你的 Python 知识,所以我在这段代码中添加了一些有用的提示:

import numpy as np
import matplotlib.pyplot as plt
from lmfit import minimize, Parameters, Parameter, report_fit

# create data to be fitted
x = np.linspace(0, 15, 301)
data = (5. * np.sin(2 * x - 0.1) * np.exp(-x*x*0.025) +
np.random.normal(size=len(x), scale=0.2) )

# define objective function: returns the array to be minimized
def fcn2min(params, x, data):

    """ model decaying sine wave, subtract data"""
    amp = params['amp'].value
    shift = params['shift'].value
    omega = params['omega'].value
    decay = params['decay'].value
    model = amp * np.sin(x * omega + shift) * np.exp(-x*x*decay)

    # tell Python we're modifying the model_data list
    # that was declared outside of this function
    global model_data

    # store the model data produced by this function call
    # add any data you want to display later to this "dictionary"
    model_data += [{
        "amp": amp,
        "shift": shift,
        "omega": omega,
        "decay": decay
    }]

    return model - data


# create a set of Parameters
params = Parameters()
params.add('amp',   value= 10,  min=0)
params.add('decay', value= 0.1)
params.add('shift', value= 0.0, min=-np.pi/2., max=np.pi/2)
params.add('omega', value= 5.0)

# declare an empty list to hold the model data
model_data = []

# do fit, here with leastsq model
result = minimize(fcn2min, params, args=(x, data))

# print each item in the model data list
for datum in model_data:
    for key in datum:

        #the 5 in %.5f controls the precision of the floating point value
        print("%s: %.5f  ") % (key, datum[key]),
    print


# calculate final result
final = data + result.residual

# try to plot results
plt.plot(x,data,'k+')
plt.plot(x,final,'r')
plt.show()

【讨论】:

  • 我使用了你的代码,然后出现错误; % 不支持的操作数类型:“Nonetype”和“元组”。我该如何治疗?
  • 我编辑了你的代码 print(("%s: %.5f ") % (key, datum[key])),它可以工作了。
  • 那么,如何只获取函数外的'amp'值??只有数据用于代码中的函数之外。
  • for datum in model_data: print(datum["amp"]) 抱歉,不知道您使用的是 Python 3
  • 那么,我可以打印最终拟合的参数,而不是整个参数吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2022-01-23
  • 2021-12-04
相关资源
最近更新 更多