Fixed tilt linalg.lstsq?

Suppose we have two datasets: x = [1,2,3] y = [2,4,6] Obviously, the linear fit will return slope 2 and intercept 0 and, of course, both procedures in Numpy linalg.lstsq()and polyfit()successfully. But they think of tilt and interception as parameters for the search.

Is it possible to keep the slope fixed and only detect interception?

+4
source share
2 answers

If the equation is a match y = a*x + b, you can find the hook bthat best matches your data, given a fixed slope a = A, like:

b = np.mean(y - A*x)

b = B , , :

a = np.dot(X, Y-B) / np.dot(X, X)
+3

scipy.optimize.fsolve:

X = np.array([1, 2, 3])
Y = np.array([2, 4, 6])
s = 2

def f(i):
    """Fixed slope 1-deg polynomial residuals"""
    return ((Y - (s*X + i))**2).sum()

, polyfit:

In [37]: np.polyfit(X, Y, 1)
Out[37]: array([  2.00000000e+00,   2.30755522e-15])

In [38]: fsolve(f, x0=1)
Out[38]: array([  1.63883763e-16])

:

In [39]: s = 4

In [40]: fsolve(f, x0=1)
Out[40]: array([-3.99075568])

+2

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


All Articles