Skip to content
Advertisement

Exponential fit in pandas

I have this data:

JavaScript

The data seems to follow an exponential curve. Let’s see the plot:

JavaScript

enter image description here

I want to fit an exponential curve ($$ y = Ae^{Bx} $$, A times e to the B*X)and add it as a column in Pandas. Firstly I tried to log the values:

JavaScript

And then to use Numpy to fit the equation:

JavaScript

But I get an error:

JavaScript

My expected result is the fitted values as a new column in Pandas. I attach an image with the column fitted values I want (in orange):

enter image description here

I’m stuck in this code. I’m not sure what I am doing wrong. How can I create a new column with my fitted values?

Advertisement

Answer

Your getting that error because np.polyfit(puf['id'],puf['log_val'],1) returns two values array([0.55110679, 6.39614819]) which isn’t the shape of your dataframe.

This is what you want

JavaScript

where

JavaScript

Giving

puf['fit'] = a * np.exp(b * puf['id'])

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement