0

I have data I am trying to fit a exponential to, this data is not ideal however when use JMP's in-build curve fit function it works as expected and a I get a good approximation of my data (please see bellow figure, JMP Fit Curve Exponential 3P).

JMP Fit Curve Exponential 3P

I am know trying to replicate this using the python library scipy.optimize with the curve_fit function as described here. However this is producing very different curves please see bellow.

import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

df = pd.read_csv('test.csv', sep = ',' ,index_col = None, engine='python')

def exponential_3p(x, a, b, c):
    return a + b * np.exp(c * x) 

popt, pcov = curve_fit(exponential_3p,df.x,df.y)

a = popt[0] 
b = popt[1]
c = popt[2]

plt.plot(df.x,df.y)
plt.plot(df.x,exponential_3p(df.x, a, b, c))

scipy optimize.curve_fit Exponential

  • You fit an exponential curve, which usually needs a good initial guess to achieve accurate fit. Other approach is to take the log of the function that you are fitting. – Péter Leéh Mar 16 '20 at 19:55
  • Any Idea how to achieve a good initial guess ? for Asymptote the mean should be close enought but not sure about the scale or growth rate b/c ? – Eoin Bolger Mar 18 '20 at 14:16

1 Answers1

1

You are yet another victim of the incomprehensible stupidity of scipy.optimize.curve_fit.

Curve-fitting and local optimization problems REQUIRES initial values for all variable parameters. They are not optional. There is no "default value" that makes sense. scipy.optimize.curve_fit lies to you about this and allows you to not provide initial values and silently (not even a warning!) assumes that you meant all initial values to be 1. This is wrong, wrong, wrong.

You must give sensible starting values or the fit.

M Newville
  • 7,486
  • 2
  • 16
  • 29
  • Thanks for your reply, with p) = [-0.26,0.0018,-2.28] i achieve the correct fit, however i need a way to achieve these initial values automatically in my case a/Asymptote a good guess here would be the mean or average of the last 10 values i was thinking. The polarity of c/growth rate could be acquired by subtracting y[-1] - y[0] however it s magnitude I'm not sure how to guess this. I'm also at a loss for what to do with b/scale. – Eoin Bolger Mar 18 '20 at 12:01