【发布时间】:2014-01-09 02:11:35
【问题描述】:
我想将平面拟合到一些数据点并绘制它。我目前的代码是这样的:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
points = [(1.1,2.1,8.1),
(3.2,4.2,8.0),
(5.3,1.3,8.2),
(3.4,2.4,8.3),
(1.5,4.5,8.0)]
xs, ys, zs = zip(*points)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs)
point = np.array([0.0, 0.0, 8.1])
normal = np.array([0.0, 0.0, 1.0])
d = -point.dot(normal)
xx, yy = np.meshgrid([-5,10], [-5,10])
z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]
ax.plot_surface(xx, yy, z, alpha=0.2, color=[0,1,0])
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
ax.set_zlim( 0,10)
plt.show()
结果如下:
正如您现在所看到的,我手动创建了平面。我该如何计算呢?我想scipy.optimize.minimize 是可能的。目前,这种错误函数对我来说并不重要。我认为最小二乘(垂直点平面距离)会很好。如果你们中的一个人能告诉我怎么做,那就太棒了。
【问题讨论】:
-
请参考Best fit plane algorithms why different results了解几种可能的方法
标签: python numpy matplotlib scipy regression