gpt4 book ai didi

python - InterX 到 python 的翻译代码出现故障

转载 作者:行者123 更新时间:2023-12-01 07:33:30 25 4
gpt4 key购买 nike

我想使用InterX函数来找到两条曲线的交点。但是该函数不会返回预期结果。该功能可用here

该函数始终返回交点,即 P = None, None。当预期有效点时。

import numpy as np
import pandas as pd
from InterX import InterX

x_t = np.linspace(0, 10, 10, True)
z_t = np.array((0, 0, 0, 0, 0, 0, 0.055, 0.41, 1.23, 4))
X_P = np.array((2,4))
Z_P = np.array((3,-1))

Line = pd.DataFrame(np.array((X_P,Z_P)))
Curve = pd.DataFrame(np.array([x_t,z_t]))
Curve = Curve.T
P = InterX(Line[0],Line[1],Curve[0],Curve[1])

在此脚本中,预期结果是 P = [3.5,0]。然而,结果点 P 是 P = [None,None]

最佳答案

简短的回答 - 使用:

P = InterX(L1, L1, L2, L2)

P = InterX(L1.iloc[:,0].to_frame(),L1.iloc[:,1].to_frame(),L2.iloc[:,0].to_frame(),L2.iloc[:,1].to_frame())

有关详细答案,请参阅以下引用您原始问题的代码的内容。

这是指original question的代码:

您需要两次传递两个具有 x 和 y 值的数据帧(如果 InterX 分别接受 4 个系列或 2 个数据帧,这当然会更合乎逻辑)。然后,InterX 以非常复杂的方式从第 90 行到第 119 行的这些数据帧中获取 x 和 y 值(这可以更容易地完成)。所以可行的解决方案是:

import numpy as np
import pandas as pd

from InterX import InterX

x_t = np.linspace(0, 10, 10, True)
z_t = np.array((0, 0, 0, 0, 0, 0, 0.055, 0.41, 1.23, 4))
x_P = np.array((2,4))
z_P = np.array((3,-1))

curve_x = pd.DataFrame(x_t)
curve_z = pd.DataFrame(z_t)
line_x = pd.DataFrame(X_P)
line_z = pd.DataFrame(Z_P)
p = InterX(line_x, line_z, curve_x, curve_z)

打印输出(p):

    xs   ys
0 3.5 0.0

请注意,根据python naming convention (PEP8) 函数和变量名称应小写,单词之间用下划线分隔。

<小时/>

我发现 InterX 的代码非常难以理解,一个更清晰的解决方案(以及漂亮的情节)是 this one 。与

x_t = np.linspace(0, 10, 10, True)
z_t = np.array((0, 0, 0, 0, 0, 0, 0.055, 0.41, 1.23, 4))
X_P = np.array((2,4))
Z_P = np.array((3,-1))

x,y = intersection(x_t,z_t,X_P,Z_P)
print(x,y)
plt.plot(x_t,z_t,c='r')
plt.plot(X_P,Z_P,c='g')
plt.plot(x,y,'*k')
plt.show()

我们得到[3.5] [-0.]和这张图片:

enter image description here

关于python - InterX 到 python 的翻译代码出现故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57110831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com