Python Forum
fit each group and extract coefficients
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fit each group and extract coefficients
#2
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. Everything
works as expected.
Reply


Messages In This Thread
RE: fit each group and extract coefficients - by scidam - Jul-20-2019, 08:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  AR roots for VAR coefficients Scott 2 1,020 Nov-30-2022, 09:23 PM
Last Post: Scott
  Neural Network importance weights / coefficients jkaustin 1 2,027 Nov-10-2020, 07:44 PM
Last Post: jefsummers
  Outputing LogisticRegression Coefficients (sklearn) RawlinsCross 6 4,656 Feb-27-2020, 02:47 PM
Last Post: RawlinsCross
  using two arrays to fit a third one - find coefficients vaugirard 1 2,576 Jul-05-2018, 10:49 PM
Last Post: Larz60+
  Printing coefficients Scott 1 5,703 Jun-30-2018, 12:14 PM
Last Post: gontajones
  How to group variables & check correlation of group variables wrt single variable SriRajesh 2 2,928 May-23-2018, 03:01 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020