Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 fit each group and extract coefficients
#1
I have a data frame df as follows:

	Board	Time	CO	H2
0	B000653BE	05.11.2018 13:28	89720	20320
1	B000653BE	05.11.2018 13:32	112760	35070
2	B000653BE	05.11.2018 13:36	130783	47063
(...)
Board	Time	CO	H2
B000653BF	08.11.2018 14:04	261254	217003
B000653BF	08.11.2018 14:08	261395	216402
B000653C2	05.11.2018 13:28	95564	49094
B000653C2	05.11.2018 13:32	90978	73274
B000653C2	05.11.2018 13:36	87743	93204
(...)
And I want to fit each group of Board via

def func_exp(x, a, b, c):
        #c = 0
        return a * np.exp(b * x) + c
        
def exponential_regression (x_data, y_data):
    popt, pcov = curve_fit(func_exp, 
                           x_data, 
                           y_data,          
                           p0 = (1000.1, 0.01, 1000000),        
                           maxfev=5000
                           )
    print(popt)   
    return func_exp(x_data, *popt)
but I can't figure out how to proceed. I thought about

df.groupby('Board').apply(exponential_regression(df.index, df["H2"]))
but I only get different errors (when I try to adjust the syntax somehow..)
I'm totally used to R, there I know how to proceed but I don't know how to do the same in python. However, I need to fit each group in regards to H2 and CO and extract the corresponding regression coefficients.

Can somebody please help me?
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to extract data between two strings SriMekala 2 176 Aug-08-2019, 01:54 PM
Last Post: SriMekala
  slice per group Progressive 3 380 Jul-20-2019, 06:52 AM
Last Post: scidam
  replace nan values by mean group by date.year, date.month wissam1974 4 279 Jul-06-2019, 02:00 AM
Last Post: wissam1974
  How to extract different data groups from multiple CSV files using python Rafiz 3 325 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  Extract of matrix subpart using a deep copy paul18fr 2 271 May-02-2019, 06:49 AM
Last Post: paul18fr
  percentage change mean by group SriRajesh 1 458 Jan-09-2019, 01:49 PM
Last Post: scidam
  OpenCV - extract 1st frame out of a video file kerzol81 2 1,623 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  Result is doutfull- fuzzywuzzy process.extract method klllmmm 0 1,136 Jul-13-2018, 01:38 AM
Last Post: klllmmm
  using two arrays to fit a third one - find coefficients vaugirard 1 519 Jul-05-2018, 10:49 PM
Last Post: Larz60+
  Printing coefficients Scott 1 917 Jun-30-2018, 12:14 PM
Last Post: gontajones

Forum Jump:


Users browsing this thread: 1 Guest(s)