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

Fitting a closed curve to a set of points

Actually, you were not far from the solution in your question. Using scipy.interpolate.splprep for parametric B-spline interpolation would be the simplest approach. It also natively supports closed curves, if you provide the per=1 parameter, import numpy as np from scipy.interpolate import splprep, splev import matplotlib.pyplot as plt # define pts from the question tck, u … Read more

Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000

Scipy’s curve_fit() uses iterations to search for optimal parameters. If the number of iterations exceeds the default number of 800, but the optimal parameters are still not found, then this error will be raised. Optimal parameters not found: Number of calls to function has reached maxfev = 800 You can provide some initial guess parameters … Read more

How do I fit a sine curve to my data with pylab and numpy?

Here is a parameter-free fitting function fit_sin() that does not require manual guess of frequency: import numpy, scipy.optimize def fit_sin(tt, yy): ”’Fit sin to the input time sequence, and return fitting parameters “amp”, “omega”, “phase”, “offset”, “freq”, “period” and “fitfunc””’ tt = numpy.array(tt) yy = numpy.array(yy) ff = numpy.fft.fftfreq(len(tt), (tt[1]-tt[0])) # assume uniform spacing Fyy … 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

Fitting a histogram with python

Here you have an example working on py2.6 and py3.2: from scipy.stats import norm import matplotlib.mlab as mlab import matplotlib.pyplot as plt # read data from a text file. One number per line arch = “test/Log(2)_ACRatio.txt” datos = [] for item in open(arch,’r’): item = item.strip() if item != ”: try: datos.append(float(item)) except ValueError: pass … Read more

Android How to draw a smooth line following your finger

An easy solution, as you mentioned, is to simply connect the points with a straight line. Here’s the code to do so: public void onDraw(Canvas canvas) { Path path = new Path(); boolean first = true; for(Point point : points){ if(first){ first = false; path.moveTo(point.x, point.y); } else{ path.lineTo(point.x, point.y); } } canvas.drawPath(path, paint); } … Read more