Scikit-Spatial中Plane.best_fit返回不合理最优拟合平面问题求助
问题原因
你遇到的现象不是平面拟合逻辑错误,是平面可视化的范围参数设置与点集实际分布不匹配导致的:
- 你传入的点集x坐标范围为20092018,y坐标范围为330,但调用
Plane.plotter时设置的lims_x=(-5, 5)、lims_y=(-5, 5)完全偏离了点集的实际分布区间,你最终看到的只是拟合平面在远离所有点的极小区域的切片,自然看起来和点集完全不匹配。 - 最优平面的拟合计算本身是正确的,你可以通过以下代码验证拟合效果:
from skspatial.objects import Plane, Points points_list = [[2018.0, 3.0, -3.0], [2016.0, 3.0, -7.0], [2014.0, 27.0, 7.0], [2013.0, 3.0, -1.5], [2012.0, 4.5, 2.0], [2012.0, 16.5, 3.5], [2012.0, 18.0, 5.5], [2010.0, 13.5, 1.0], [2010.0, 21.0, -3.0], [2009.0, 30.0, 4.5]] points = Points(points_list) plane = Plane.best_fit(points) # 输出所有点到拟合平面的距离,会发现数值都非常小,拟合符合预期 for idx, pt in enumerate(points): print(f"第{idx+1}个点到平面距离:{plane.distance_point(pt):.3f}")
修正后的可视化代码
将平面绘制的范围调整为点集实际分布区间即可:
from skspatial.objects import Plane, Points from skspatial.plotting import plot_3d points_list = [[2018.0, 3.0, -3.0], [2016.0, 3.0, -7.0], [2014.0, 27.0, 7.0], [2013.0, 3.0, -1.5], [2012.0, 4.5, 2.0], [2012.0, 16.5, 3.5], [2012.0, 18.0, 5.5], [2010.0, 13.5, 1.0], [2010.0, 21.0, -3.0], [2009.0, 30.0, 4.5]] plot_3d( Points(points_list).plotter(c='k',s=15,depthshade=True), Plane.best_fit(points_list).plotter(alpha=0.2, lims_x=(2008, 2019), lims_y=(0, 35)) )[0].show()
内容的提问来源于stack exchange,提问作者Elliott Liu
相关产品推荐
相关产品推荐

