0

I have been using lmfit for about a day now and needless to say I know very little about the library. I have been using several built-in models for curve fitting and all of them work flawlessly with the data except the Lognormal Model.

Here is my code:

from numpy import *
from lmfit.models import LognormalModel
import pandas as pd
import scipy.integrate as integrate

import matplotlib.pyplot as plt

data = pd.read_csv('./data.csv', delimiter = ",")
x = data.ix[:, 0]
y = data.ix[:, 1]

print (x)
print (y)

mod = LognormalModel()
pars = mod.guess(y, x=x)
out = mod.fit(y, pars , x=x)
print(out.best_values)
print(out.fit_report(min_correl=0.25))
out.plot()

plt.plot(x, y,         'bo')
plt.plot(x, out.init_fit, 'k--')
plt.plot(x, out.best_fit, 'r-')
plt.show()

and the error output is:

Traceback (most recent call last):
  File "Cs_curve_fit.py", line 17, in <module>
    pvout = pvmod.fit(y, amplitude= 1, center = 1, sigma =1 , x=x)
  File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\model.py", line 731, in fit
    output.fit(data=data, weights=weights)
  File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\model.py", line 944, in fit
    self.init_fit = self.model.eval(params=self.params, **self.userkws)
  File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\model.py", line 569, in eval
    return self.func(**self.make_funcargs(params, kwargs))
  File "C:\Users\NAME\Anaconda3\lib\site-packages\lmfit\lineshapes.py", line 162, in lognormal
    x[where(x <= 1.e-19)] = 1.e-19
  File "C:\Users\NAME\Anaconda3\lib\site-packages\pandas\core\series.py", line 773, in __setitem__
    setitem(key, value)
  File "C:\Users\NAME\Anaconda3\lib\site-packages\pandas\core\series.py", line 755, in setitem
    raise ValueError("Can only tuple-index with a MultiIndex")
ValueError: Can only tuple-index with a MultiIndex

1 Answers1

0

First, the error message you show cannot have come from the code you post. The error message says that line 17 of the file "Cs_curve_fit.py" reads

pvout = pvmod.fit(y, amplitude= 1, center = 1, sigma =1 , x=x)

but that is not anywhere in your code. Please post the actual code and the actual output.

Second, the problem appears to happening because the data for x is cannot be turned into a 1D numpy array. Not being able to trust your code or output, I would just suggest converting the data to 1D numpy arrays yourself as a first test. Lmfit should be able to handle Pandas series, but it just does a simple coercion to 1D numpy arrays.

M Newville
  • 7,486
  • 2
  • 16
  • 29