【问题标题】:How to align two hyperplanes with plane equations如何用平面方程对齐两个超平面
【发布时间】: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


    【解决方案1】:

    如果你只是想在相同的 3d 轴上绘制两个平面,那么你只需要为 Z 计算两个不同的值并绘制两个 Zs 相对于相同的 Xs 和 Ys,像这样:

    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    
    # first plane:
    
    a1 = -5.297742252442251
    b1 = 21.751836101364013
    c1 = -2.470896764133499
    d1 = -0.5601826186620921
    
    # second plane:
    
    a2 = 45.42557999642176
    b2 = -16.9433283673388
    c2 = 2.5117971500097287
    d2 = -8.528560240570203
    
    x = np.linspace(-1, 1, 100)
    y = np.linspace(-1, 1, 100)
    
    X, Y = np.meshgrid(x, y)
    Z1 = (-d1 - a1 * X - b1 * Y) / c1
    Z2 = (-d2 - a2 * X - b2 * Y) / c2
    
    fig = plt.figure(figsize=(10, 10))
    ax = plt.axes(projection="3d")
    ax.plot_surface(X, Y, Z1, alpha=0.45)
    ax.plot_surface(X, Y, Z2, alpha=0.45)
    plt.show()
    

    这会产生下图:

    至于“对齐”两架飞机,我不清楚你在问什么......?如果两个平面在您上面给出的等式中具有相同的 a、b、c 和 d 值,则它们将共面 - 如果它们具有不同的 a、b、c 和 d 值,则它们将不共面。它们可能相交,也可能不相交——为了确保你在它们相交的地方画出它们(如果它们这样做的话),你需要确定 x 和 y 的值,它们具有相等的 z 值,并相应地设置你的 xrange 和 yrange .如果您可以为您的特定目的解释“对齐”的含义,也许我可以扩展这个答案以适应。

    【讨论】:

    • 哦,我试图对齐两个由线性歧视创建的超平面。所以我想在变换平面时变换平面两侧的所有数据点。我编辑了问题以使用超平面显示数据点
    • 你能为你所说的“转变”制定你的算法/过程吗?你能提供什么示例数据点?我看到你提供了一张图片——但如果你想让代码处理一些实际的点,请提供这些点和你想对这些点做什么的描述?
    • 我在问题中添加了一些数据点。我想将这两个数据之一转换为超平面在一个坐标系中对齐的位置。我想知道如何将数据点转换为两个超平面对齐的平面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2023-03-27
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多