Python Forum
how to fit an exponential function to multiple data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to fit an exponential function to multiple data
#1
I would like to fit an exponential function based on data points of more than one trend. Is this possible?
The code below creates a fit for every column in the table but if I want to group these columns according to a certain criteria so say the points in column A, B and C needs to learn the same exponential fit, i am not very sure how to do incorporate that requirement here. Thanks.

# Initial parameter guess, just to kick off the optimization
init = (0.1, 0.1)

# Place to store function parameters for each column
col_params = {}

def func(x, a, b):
    return a*np.exp(-b*x)

# Curve fit each column
for col in df.columns:
    # Get x & y
    x = df.index.astype(float).values
    y = df[col].values
    
    # Curve fit column and get curve parameters
    params = curve_fit(func, x, y, init)
      
    # Store optimized parameters
     col_params[col] = params[0]
        

# Extrapolate each column
for col in df.columns:
    # Get the index values for NaNsY in the column
    x = df[pd.isnull(df[col])].index.astype(float).values 
    y = df[col].values

    # Extrapolate those points with the fitted function
    df[col][x] = func(x, *col_params[col])
    plt.plot(x1,y,func(x, *col_params[col]),'g--')
            
Reply
#2
Not sure about what you are trying to get, but take a look at RANSAC algorithm. It allows to find the best fit, as if A, B and C in your case were undistinguished. There is a Python implementation of RANSAC. However, it uses a while loop when finding the best combination, so, it would be probably better (more efficient) to rewrite all code using Cython (or C).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Laplacian Exponential Diffusion Kernel ainisyarifaah 1 1,692 Nov-25-2021, 06:21 PM
Last Post: Gribouillis
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 2,131 Jun-09-2020, 06:05 AM
Last Post: Pan
  Least-squares fit multiple data sets multiverse22 1 2,226 Jun-06-2020, 01:38 AM
Last Post: Larz60+
  How to extract different data groups from multiple CSV files using python Rafiz 3 3,196 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  Differentiation with exponential functions tyrael69 0 3,976 Feb-09-2018, 02:01 AM
Last Post: tyrael69
  help on exponential smoothing and linear regression hkyeung 1 3,105 Sep-02-2017, 09:31 PM
Last Post: Larz60+
  Panda Data Frame to Existing Multiple Sheets naveedraza 1 5,634 Jul-11-2017, 12:21 PM
Last Post: naveedraza
  Write data into existing Excel (xlsx) file with multiple sheets BNB 1 15,306 Jun-01-2017, 04:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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