Search code examples
python-3.5lmfit

confusing error when using minimize of lmfit in python


my code is as following:

import numpy as np
from math import *
from scipy.optimize import *
import scipy.optimize as opt
from lmfit import Minimizer, Parameters, report_fit
import lmfit as lf

f = open('data.txt','r')
lines=f.readlines()   

n1=[]
n2=[]
n=[]
h=[]
for x in lines:
    x=x.strip() # remove \n before splitting the line
    n1.append(x.split('\t')[0])
    n2.append(x.split('\t')[1])
    n.append(x.split('\t')[2])
    h.append(x.split('\t')[3])
f.close()

n1 = [float(i) for i in n1]
n2 = [float(i) for i in n2]
n = [float(i) for i in n]
h = [float(i) for i in h]
# convert a list into an array
n1 = np.array(n1)
n2 = np.array(n2)
n = np.array(n)
h = np.array(h)

def fith(params,n1,n2,n,h):
    a1 = params['p1']
    b1 = params['p2']
    a2 = params['p3']
    b2 = params['p4']
    model = (a1 + b1*n) * n1 + (a2 + b2*n) * n2
    return model - h

params = Parameters()
params.add('p1',value=1.0)
params.add('p2',value=1.0)
params.add('p3',value=1.0)
params.add('p4',value=1.0)

out = minimize(fith,params,args=(n1,n2,n,h))

print(out)

after run, I got the error as following:

#

Traceback (most recent call last): File "E:\new model\calculate_H_v2.py", line 50, in out = minimize(fith,params,args=(n1,n2,n,h)) File "E:\softwares\python\lib\site-packages\scipy\optimize_minimize.py", line 481, in minimize return _minimize_bfgs(fun, x0, args, jac, callback, **options) File "E:\softwares\python\lib\site-packages\scipy\optimize\optimize.py", line 943, in _minimize_bfgs gfk = myfprime(x0) File "E:\softwares\python\lib\site-packages\scipy\optimize\optimize.py", line 292, in function_wrapper return function((wrapper_args + args)) File "E:\softwares\python\lib\site-packages\scipy\optimize\optimize.py", line 703, in approx_fprime return _approx_fprime_helper(xk, f, epsilon, args=args) File "E:\softwares\python\lib\site-packages\scipy\optimize\optimize.py", line 637, in _approx_fprime_helper f0 = f(((xk,) + args)) File "E:\softwares\python\lib\site-packages\scipy\optimize\optimize.py", line 292, in function_wrapper return function(*(wrapper_args + args)) File "E:\new model\calculate_H_v2.py", line 35, in fith a1 = params['p1'] IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

#

I could not figure it out why I got this kind of error after read through the questions. Can you help me out?

Thanks in advance.

Jing


Solution

  • I think the basic problem is that you are using scipy.optimize.minimize() instead of lmfit.minimize(). That is, you import * from scipy.optimize, then you import Minimizer from lmfit.

    That is, using from lmfit import minimize, Parameters, report_fit, or use

    mini = Minimizer(fith,params,args=(n1,n2,n,h))
    out = mini.minimize()
    

    should make your script use lmfit.minimize() which looks to me like it should then work.

    This is why import * is painful: you have a hard time telling where symbols came from.

    As a side comment, you can probably use numpy.loadtxt() to simplify the reading of your data to numpy arrays.