【发布时间】:2023-01-30 05:01:47
【问题描述】:
我正在尝试将一个平面转换为另一个平面的欧几里得。我有两个平面方程,它们有不同的大小。如何在一个坐标系中对齐两个平面?
我的两架飞机的形式是ax+by+cz+d=0。
第一架飞机 => a = -5.297742252442251,b = 21.751836101364013,c = -2.470896764133499,d = -0.5601826186620921
第二个平面 => a = 45.42557999642176,b = -16.9433283673388,c = 2.5117971500097287,d = -8.528560240570203]
为了在 matplotlib 上绘图,我使用了以下使用 matplotlib 的代码
import numpy as np
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from skimage import measure
import pandas as pd
# Data points 1 and 2
data1 = [[0.190133571624755, 0.146549582481384, 0.391435742378234, 'near'],
[0.0154470205307006, 0.0959569215774536, 0.484999418258667, 'near'],
[-0.119875073432922, 0.0414541959762573, 0.542818903923034, 'near'],
[0.104917883872985, 0.058539867401123, 0.171926498413085, 'far'],
[0.177520513534545, 0.130982756614685, 0.0330302715301513, 'far'],
[0.246979117393493, 0.173633933067321, 0.373323440551757, 'far']]
data2 = [[0.334545135498046, -0.0318257808685302, 0.282101511955261, 'near'],
[0.411889553070068, 0.0223467350006103, 0.183727979660034, 'near'],
[0.330880641937255, -0.00959080457687378, 0.178299665451049, 'near'],
[-0.00756144523620605, -0.07442307472229, -0.227764248847961, 'far'],
[-0.268512785434722, -0.309048891067504, 0.456292867660522, 'far'],
[-0.305409669876098, -0.304299354553222, 0.281461238861084, 'far']]
# Create the pandas DataFrame
df1 = pd.DataFrame(data1, columns=['A', 'B', 'C', 'NearOrFar'])
df2 = pd.DataFrame(data2, columns=['A', 'B', 'C', 'NearOrFar'])
# Data - 1
# Divide into X and y
X1 = df1.iloc[:,0:3]
Y1 = df1.iloc[:,3]
# Create scatter plot of data points for data 1
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')
for grp_name, grp_idx in df1.groupby('NearOrFar').groups.items():
x = df1.iloc[grp_idx, 0]
y = df1.iloc[grp_idx, 1]
z = df1.iloc[grp_idx, 2]
if (grp_name == 'near'):
ax.scatter(x, y, z, label=grp_name, c = 'red')
else:
ax.scatter(x, y, z, label=grp_name, c = 'blue')
# Train LDA model for data 1
lda_clf_1 = LDA(store_covariance=True)
lda_clf_1.fit(X1, Y1)
# Decision boundary Coefficient
a,b,c,d = lda_clf_1.coef_[0][0],lda_clf_1.coef_[0] [1],lda_clf_1.coef_[0][2],lda_clf_1.intercept_
# Find limit of each coordinates
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Create meshgrid in xyz
xx = np.linspace(xlim[0], xlim[1], 50)
yy = np.linspace(ylim[0], ylim[1], 50)
X,Y = np.meshgrid(xx,yy)
Z = (-d - a*X - b*Y) / c
# plot decision boundary hyperplane
ax.plot_surface(X, Y, Z, alpha=0.45)
plt.show()
# Data - 2
# Divide into X and y
X2 = df2.iloc[:,0:3]
Y2 = df2.iloc[:,3]
# Create scatter plot of data points for data 2
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')
for grp_name, grp_idx in df2.groupby('NearOrFar').groups.items():
x = df2.iloc[grp_idx, 0]
y = df2.iloc[grp_idx, 1]
z = df2.iloc[grp_idx, 2]
if (grp_name == 'near'):
ax.scatter(x, y, z, label=grp_name, c = 'red')
else:
ax.scatter(x, y, z, label=grp_name, c = 'blue')
# Train LDA model for data 2
lda_clf_2 = LDA(store_covariance=True)
lda_clf_2.fit(X2, Y2)
# Decision boundary Coefficient
a,b,c,d = lda_clf_2.coef_[0][0],lda_clf_2.coef_[0][1],lda_clf_2.coef_[0][2],lda_clf_2.intercept_
# Find limit of each coordinates
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Create meshgrid in xyz
xx = np.linspace(xlim[0], xlim[1], 50)
yy = np.linspace(ylim[0], ylim[1], 50)
X,Y = np.meshgrid(xx,yy)
Z = (-d - a*X - b*Y) / c
# plot decision boundary hyperplane
ax.plot_surface(X, Y, Z, alpha=0.45)
plt.show()
如何对齐两个平面并在一个带有数据点的图表中创建两个对齐平面的 3d 图?
最后,我想将 data2 上的所有数据点转换(我认为主要是旋转?)到 data1 的坐标系,因为 data2 超平面与 data1 超平面对齐
每个带有超平面的数据点应该如下所示
数据 1 = enter image description here
和 数据 2 = enter image description here
【问题讨论】:
标签: python matplotlib transformation