BaRud
BaRud

Reputation: 3218

extrapolate pandas dataframe with lmfit

I am trying to extrapolate my data

14  , 18   , 38   , 57   , 100  , 130  , 191
18  , 26   , 48   , 74   , 79   , 130  , 165
3   , 3    , 3    , 3    , 3    , 3    , 6  
323 , 470  , 655  , 889  , 1128 , 1701 , 203
0   , 0    , 0    , 0    , 1    , 1    , 1  
977 , 1261 , 1766 , 2337 , 3150 , 3736 , 433
9   , 13   , 25   , 33   , 58   , 84   , 120

as:

import matplotlib.pyplot as plt
import numpy as np
import pandas
from lmfit import Model
from lmfit.models import ExponentialModel, LorentzianModel, PolynomialModel
from pandas.plotting import scatter_matrix


names = ["A", "B", "C", "D", "E", "F", "G"]

data = (np.genfromtxt("trial.dat", delimiter=','))
dataset = pandas.DataFrame(data=data.T, columns=names)
x=dataset.index.values
x_pre=np.linspace(0,10, 10)
print(x_pre)
model = PolynomialModel(5)
for name in names:
  print(name)
  color = next(plt.gca()._get_lines.prop_cycler)['color']
  params = model.guess(dataset[name], x=x)
  result = model.fit(dataset[name], params, x=x)
  plt.plot(dataset.index.values, dataset[name], 'o', color=color, label=name)
  #  plt.plot(dataset.index.values, result.best_fit, '-', color=color)
  pred = model.eval(x=x_pre)
  print(pred)
  #  print(result.fit_report())
plt.legend(loc='best')
plt.show()  

as defined here

You can apply this composite model to other data sets, or evaluate the model at other values of x. You may want to do this to give a finer or coarser spacing of data point, or to extrapolate the model outside the fitting range. This can be done with:

xwide = np.linspace(-5, 25, 3001) predicted = mod.eval(x=xwide)

but, this is not working, as pred is returning 0 only.

So, how I can extrapolate the data, fitted with lmfit?

Upvotes: 0

Views: 351

Answers (1)

M Newville
M Newville

Reputation: 7862

You probably want to use

 pred = result.eval(x=x_pre)

That is, you want to use the result of the fit, not the model itself.

Upvotes: 1

Related Questions