Fit sigmoid function (“S” shape curve) to data using Python

After great help from @Brenlla the code was modified to: def sigmoid(x, L ,x0, k, b): y = L / (1 + np.exp(-k*(x-x0))) + b return (y) p0 = [max(ydata), np.median(xdata),1,min(ydata)] # this is an mandatory initial guess popt, pcov = curve_fit(sigmoid, xdata, ydata,p0, method=’dogbox’) The parameters optimized are L, x0, k, b, who are … Read more

Curve Fitting to a time series in the format ‘datetime’?

Instead of plotting datenums, use the associated datetimes. import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import datetime as DT import time dates = [DT.datetime(1978, 7, 7), DT.datetime(1980, 9, 26), DT.datetime(1983, 8, 1), DT.datetime(1985, 8, 8)] y = [0.00134328779552718, 0.00155187668863844, 0.0039431374327427, 0.00780037563783297] yerr = [0.0000137547160254577, 0.0000225670232594083, 0.000105623642510075, 0.00011343121508] x = mdates.date2num(dates) … Read more

fitting data with numpy

Unfortunately, np.polynomial.polynomial.polyfit returns the coefficients in the opposite order of that for np.polyfit and np.polyval (or, as you used np.poly1d). To illustrate: In [40]: np.polynomial.polynomial.polyfit(x, y, 4) Out[40]: array([ 84.29340848, -100.53595376, 44.83281408, -8.85931101, 0.65459882]) In [41]: np.polyfit(x, y, 4) Out[41]: array([ 0.65459882, -8.859311 , 44.83281407, -100.53595375, 84.29340846]) In general: np.polynomial.polynomial.polyfit returns coefficients [A, B, C] … Read more

How do I put a constraint on SciPy curve fit?

You can define your own residuals function, including a penalization parameter, like detailed in the code below, where it is known beforehand that the integral along the interval must be 2.. If you test without the penalization you will see that what your are getting is the conventional curve_fit: import matplotlib.pyplot as plt import scipy … Read more