简体   繁体   English

在python中使用lmfit模块时,如何在模型中调用参数?

[英]With using lmfit module in python, how to call the parameters in the model?

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

In this code, I want to call the parameters like 'amp', 'shift' in the python. 在这段代码中,我想在python中调用“ amp”,“ shift”之类的参数。 Print(amp).. kinds of things How to call these parameters in the python after fitting? Print(amp)..各种各样的东西拟合后如何在python中调用这些参数? When I use print(amp), the error message is shown; 当我使用print(amp)时,将显示错误消息。 name 'amp' is not defined. 未定义名称“ amp”。 How to print these fitted parameters using print function? 如何使用打印功能打印这些适合的参数? (etc. print(amp)) (等等。print(amp))

You are likely trying to print that data outside of the function. 您可能试图在功能之外打印该数据。 The amp , shift , omega and decay variables are in fc2min 's local scope and are therefore only accessible inside the function. ampshiftomegadecay变量位于fc2min的本地范围内,因此只能在函数内部访问。 Your data analysis skills seem to far outmatch your Python know-how, so I added some helpful tips inside this code: 您的数据分析技能似乎远远超出了您的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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM