我正试图在scikit中使用RANSAC将飞机安装到点云上。
我无法理解如何操作,如何绘制我从ransac.predict
获得的平面。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets, linear_model
diabetes = datasets.load_diabetes()
X_train = diabetes.data[:-20, (0,1)]
y_train = diabetes.target[:-20]
ransac = linear_model.RANSACRegressor(
linear_model.LinearRegression()
)
ransac.fit(X_train, y_train)
fig = plt.figure()
plt.clf()
ax = Axes3D(fig)
ax.plot_surface([-5,5],[-5,5], ransac.predict(X_train))
我收到错误消息
ValueError: shape mismatch: objects cannot be broadcast to a single shape
答案 0 :(得分:1)
在此示例中,您仅使用2个要素来拟合,不是PLANE而是一条线。
这也可以从以下位置看到:
ransac.estimator_.coef_
array([266.63361536, -48.86064441])
包含您拥有的两个功能的权重。
让我们制作一个真实的3D案例:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets, linear_model
diabetes = datasets.load_diabetes()
X_train = diabetes.data[:-20, (0,1,2)]
y_train = diabetes.target[:-20]
ransac = linear_model.RANSACRegressor(linear_model.LinearRegression())
ransac.fit(X_train, y_train)
# the plane equation
z = lambda x,y: (-ransac.estimator_.intercept_ - ransac.estimator_.coef_[0]*x - ransac.estimator_.coef_[1]*y) / ransac.estimator_.coef_[2]
tmp = np.linspace(-5,5,50)
x,y = np.meshgrid(tmp,tmp)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(X_train[:,0], X_train[:,1], X_train[:,2], 'or')
ax.plot_surface(x, y, z(x,y))
ax.view_init(10, 60)
plt.show()