model = Model(fun, independent_vars=['A', 'B'])
data = zip(df['A'].tolist(), df['B'].tolist())
data = np.array(data).flatten()I'm trying to fit a modelmodel = Model(fun, independent_vars=['A', 'B'])where A and B are Series in a Pandas DataFrame. The model should be fit against the Series "target" from that dataframe.How do I need to prepare the actual data that I pass to the fit() function?
From the FAQ I see that I probably need to flatten it, so does this mean I need to first zip all the series, then flatten that array? Like:data = zip(df['A'].tolist(), df['B'].tolist())
data = np.array(data).flatten()
But then my independent variable data is four times longer than the target value list.Could someone please give a simple example on how to curve fit with multiple independent variables?
import pandas as pd
import numpy as np
from lmfit import Model
df = pd.DataFrame({
'A' : pd.Series([1, 1, 1, 2, 2, 2, 2]),
'B' : pd.Series([5, 4, 6, 6, 5, 6, 5]),
'target' : pd.Series([87.79, 40.89, 215.30, 238.65, 111.15, 238.65, 111.15])
})
def fun(A, B, p1 = 1, p2 = 1):
return p1 * np.exp(A) + p2 * np.exp(B)
model = Model(fun, independent_vars=['A', 'B'])
fit = model.fit(df['target'], A = df['A'], B = df['B'])
fit.eval()