skspatial.objects.Plane.best_fit¶
- classmethod Plane.best_fit(points: array_like, tol: Optional[float] = None, return_error: bool = False, **kwargs) Plane | tuple[Plane, float] [source]¶
Return the plane of best fit for a set of 3D 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 two).
“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 3D 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:
- Plane | tuple[Plane, float]
The plane of best fit, and optionally the error of the fit.
- Raises:
- ValueError
If the points are collinear or are not 3D.
References
[1]: “Singular Value Decomposition”, Oracle, https://docs.oracle.com/en/database/oracle/machine-learning/oml4sql/23/dmcon/singular-value-decomposition.html#GUID-14AA4B45-3B36-4056-9B9A-BD9DC471F0AD
Examples
>>> from skspatial.objects import Plane
>>> points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]] >>> plane = Plane.best_fit(points)
The point on the plane is the centroid of the points.
>>> plane.point Point([0.25, 0.25, 0.25])
The plane normal is a unit vector.
>>> plane.normal.round(3) Vector([-0.577, -0.577, -0.577])
>>> points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]]
>>> Plane.best_fit(points) Plane(point=Point([0.5, 0.5, 0. ]), normal=Vector([0., 0., 1.]))
>>> Plane.best_fit(points, full_matrices=False) Plane(point=Point([0.5, 0.5, 0. ]), normal=Vector([0., 0., 1.]))