0

my dataframe [11 x 300], where the column header equals 'x' ([0.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25]), and each row-value represents 'y' for. Each row can be described by an exponential function in the following format : a * x ^k + b.

The goal is to add three additional columns, describing a, k and b for that specific row. Just like: Python curve fitting on pandas dataframe then add coef to new columns

Instead of a polynomial function, my data needs be described in the following format: a * x **k + b.

As I cannot find any solution to derive the coefficients by using np.polyfit, I split my dataframe into different lists.


x  = np.array([0.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25])
y1  = np.array([288.79,238.32,199.42,181.22,165.50,154.74,152.25,152.26,144.81,144.81,144.81])
y2 = np.array([309.92,255.75,214.02,194.48,177.61,166.06,163.40,163.40,155.41,155.41,155.41])
...
y300 = np.array([352.18,290.63,243.20,221.00,201.83,188.71,185.68,185.68,176.60,176.60,176.60])

def func(x,a,k,b):
    return a * (x**k) + b

popt1, pcov = curve_fit(func,x,y1, p0 = (300,-0.5,0))
...
popt300, pcov = curve_fit(func,x,y300, p0 = (300,-0.5,0))

output: 
popt1 
[107.73727907  -1.545475   123.48621504]
...
popt300
[131.38411712  -1.5454452  150.59522147

This works, when I split all dataframe rows into lists and define popt for every list/row. Avoiding to split all 300 columns - I prefer to apply the same methodology as Python curve fitting on pandas dataframe then add coef to new columns


my_coep_array = pd.DataFrame(np.polyfit(x, df.values,1)).T


But how to define my np.polyfit - a * x **k + b?

Quang Hoang
  • 117,517
  • 10
  • 34
  • 52

0 Answers0