Jul-20-2019, 08:20 AM
exponential_regression
function should return a list (an array) of coefficients. But you return y-value estimations. I slightly restructure the code:
from scipy.optimize import curve_fit import pandas as pd c = pd.np.random.choice(range(1, 5), 1000) df = pd.DataFrame({'Board': c, 'x':pd.np.linspace(2, 10, 1000), 'y': 2+c*pd.np.exp(pd.np.linspace(2, 10, 1000))})
df
is a sample data frame, it contains 'board' variable. This variable takes random values 1, ... , 4. This is just sample data.def func_exp(x, a, b, c): #c = 0 return a * pd.np.exp(b * x) + c def exponential_regression (x_data, y_data): popt, pcov = curve_fit(func_exp, x_data, y_data, p0 = (1,1,1), maxfev=5000 ) return popt
res = df.groupby('Board').apply(lambda x: exponential_regression(x['x'], x['y'])) res
Output:Board
1 [1.0, 1.0, 2.0000000000021814]
2 [1.9999999999999998, 0.9999999999999998, 1.999...
3 [3.000000000000001, 0.9999999999999997, 2.0000...
4 [3.9999999999999956, 1.0000000000000002, 2.000...
dtype: object
So, the first value consequently takes 1, 1.9999, ... ,3.99999. These are coefficients a
for our groups of data. Everythingworks as expected.