Limited np.polyfit

I am trying to match quadratic data with some experimental data and use polyfit in numpy. I want to get a concave curve and, therefore, I want to make sure that the coefficient of the quadratic term is negative, and also the weighted weight itself, since it has several weights. Is there an easy way to do this? Thanks.

+5
source share
2 answers

The use of weights is described here (numpy.polyfit) . Basically, you need a weight vector with the same length as x and y.

To avoid the wrong sign in the coefficient, you can use the definition of the fit function, for example

def fitfunc(x,a,b,c): return -1 * abs(a) * x**2 + b * x + c 

This will give you a negative coefficient for x ** 2 at any time.

+1
source

You can use curve_fit .

Or you can run a polyfit with a rank of 2, and if the last coefficient is greater than 0. run linear polyfit again (a polyphit with a rank of 1)

0
source

Source: https://habr.com/ru/post/1206355/


All Articles