我一直在使用lmfit大约一天,不用说我对图书馆知之甚少。我一直在使用几个内置的曲线拟合模型,除了Lognormal模型之外,它们都可以完美地处理数据。
这是我的代码:
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()
,错误输出为:
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
答案 0 :(得分:0)
首先,您显示的错误消息不能来自您发布的代码。错误消息显示文件的第17行&#34; Cs_curve_fit.py&#34;读取
pvout = pvmod.fit(y, amplitude= 1, center = 1, sigma =1 , x=x)
但这不是代码中的任何地方。请发布实际代码和实际输出。
其次,问题似乎正在发生,因为x
的数据无法转换为1D numpy数组。无法信任您的代码或输出,我建议您自己将数据转换为1D numpy数组作为第一个测试。 Lmfit应该能够处理Pandas系列,但它只是对1D numpy数组进行简单的强制。