簡體   English   中英

翻譯代碼 InterX 到 python 的故障

[英]Malfunction of the translated code InterX to python

我想使用函數 InterX 來找到兩條曲線的交點。 但是,該函數不會返回預期的結果。 該功能在此處可用

該函數始終將交點返回為 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())

有關詳細答案,請參閱以下內容,其中涉及原始問題的代碼。

這是指原始問題的代碼:

您需要兩次傳遞具有 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命名約定(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.]和這張圖片:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM