skspatial.objects.Line.best_fit

classmethod Line.best_fit(points: array_like, tol: Optional[float] = None, return_error: bool = False, **kwargs) Line | tuple[Line, float][source]

Return the line of best fit for a set of points.

Also optionally return a value representing the error of the fit. This is the sum of the squared singular values from SVD (excluding the first).

“The singular values reflect the amount of data variance captured by the bases. The first basis (the one with largest singular value) lies in the direction of the greatest data variance. The second basis captures the orthogonal direction with the second greatest variance, and so on.” [1]

Parameters:
pointsarray_like

Input points.

tolfloat | None, optional

Keyword passed to Points.are_collinear() (default None).

return_errorbool, optional

If True, also return a value representing the error of the fit (default False).

kwargsdict, optional

Additional keywords passed to numpy.linalg.svd()

Returns:
Line | tuple[Line, float]

The line of best fit, and optionally the error of the fit.

Raises:
ValueError

If the points are concurrent.

References

Examples

>>> from skspatial.objects import Line
>>> points = [[0, 0], [1, 2], [2, 1], [2, 3], [3, 2]]
>>> line = Line.best_fit(points)

The point on the line is the centroid of the points.

>>> line.point
Point([1.6, 1.6])

The line direction is a unit vector.

>>> line.direction.round(3)
Vector([0.707, 0.707])