I am trying to use the lambda function in pandas to apply a curve fit function (using the lmfit library) to data which is stored in a dataframe.
I'm struggling to get the syntax right on the delta function though, and can't seem to figure out where to use 'rows' to point at the correct data.
The code I have tried to implement is below:
from lmfit import minimize, Parameters
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# define function for fitting the data, from Vogel
def get_residual(row, f_mat, W_mmin, W_mkin, S):
k = 4 # Weibull parameter
x = row['x']
model = 1 - np.exp(-f_mat * (x) * k * (W_mkin - W_mmin))
return S - model
# define parameters required for curve fit and initial guess values
params = Parameters()
params.add('f_mat', value = 0)
params.add('W_mmin', value = 0)
# import data
data = pd.read_csv('wpd_datasets (5).csv')
# insert into dataframe
Size_Data = pd.DataFrame({
'x': [8e-3, 4e-3],
'Wmkin' : [[0.070247308,0.110537403, 0.150960721, 0.258617823, 0.322579856], [0.100385814, 0.200407372, 0.310034683, 0.440546697, 0.591512151, 0.794210076]],
'S' : [[0.07053083175, 0.3514834285, 0.6014003444, 0.9091664401, 0.9980905768], [0.4077464576, 0.2298558869, 0.5899090607, 0.8002673797, 0.910358319, 0.94841536]]
})
Size_Data['parameters'] = Size_Data.apply(lambda row: minimize(get_residual, params, args = (row, params, row['W_mkin'], row['S'])), axis = 1)
the problem occurs in the function where I try to access the row again to find x, but I get a key error. The row function is looking at the parameters function from lmfit but I have no idea how to fix it. How do I access the rows x value in the dataframe, within the function?