![](/img/trans.png)
[英]finding the interpolated intersection between two arrays in Python/Numpy/Scipy
[英]Finding the point of intersection of 3 Numpy Arrays Python
我正在嘗試編寫 function 代碼,它為我提供list_2
或list_3
與 list_ 交叉的list_
。 因此,如果 numpy 代碼中有任何交點,它將給我交點。 我想按順序獲得十字架,因此必須對索引列表進行格式化,以便按照list_2 cross, list_3 cross, list_2 cross
或list_3 cross, list_2 cross, list_3 cross
等順序給我一個十字架。所以如果發生了交叉,它必須等待其他數組值通過list
才能通過 go。 I do not know how I can go through with this though I have tried using a numpy.where()
function and such, I am also using the pandas module so if it has a valid function for this I could use that too.
變量:
list_ = np.array([9887.89 9902.99 9902.99 9910.23 9920.79 9911.34 9920.01 9927.51 9932.3
9932.33 9928.87 9929.22 9929.22 9935.24 9935.24 9935.26 9935.26 9935.68
9935.68 9940.5 ])
list_2 = np.array([9935.26 9935.26 9935.68 9935.68 9940.5 9925.19 9925.19 9929.62 9929.65
9929.93 9932.55 9936.81 9936.84 9937.26 9932.55 9932.55 9932.55 9932.6
9932.6 9932.6])
list_3_ = np.array([9928.87 9929.22 9929.22 9935.24 9935.24 9935.26 9935.26 9935.68 9935.68
9940.5 9925.19 9925.19 9929.62 9929.65 9929.93 9932.55 9936.81 9936.84
9937.26 9932.55])
plot:
預期 Output:
List_2 cross at 5, List_3 cross at 10, List_2 cross at 14, List_3 cross at 15, List_2 cross at 18, List_3 cross at 19
兩個系列a
和b
之間的交叉點或交叉索引是索引i
其中:
b
從上面穿過a
)b
從下方穿過a
)a
和b
接觸)因此,我們可以通過比較每個數組的“當前”(第i個)和“下一個”(第i+1個)值來獲得索引。
def intersection_points(a, *others):
if a.ndim != 1 or any(other.shape != a.shape for other in others):
raise ValueError('The arrays must be single dimensional and the same length')
others = np.array(others)
indices = np.argwhere(
((a[:-1] < others[..., :-1]) & (a[1:] > others[..., 1:])) |
((a[:-1] > others[..., :-1]) & (a[1:] < others[..., 1:])) |
(a[:-1] == others[..., :-1]))
return indices[indices[:, 1].argsort()] # sort by i
a = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
b = np.array([9935.26, 9935.26, 9935.68, 9935.68, 9940.5, 9925.19, 9925.19, 9929.62, 9929.65, 9929.93, 9932.55, 9936.81, 9936.84, 9937.26, 9932.55, 9932.55, 9932.55, 9932.6, 9932.6, 9932.6])
c = np.array([9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5, 9925.19, 9925.19, 9929.62, 9929.65, 9929.93, 9932.55, 9936.81, 9936.84, 9937.26, 9932.55])
print(intersection_points(a, b, c))
這將返回以下格式的交點數組:
[[ 0 7]
[ 0 9]
[ 1 9]
[ 1 11]
[ 1 12]
[ 0 13]
[ 1 15]
[ 1 18]]
這意味着b
(您的list_2
)在索引 7、9、13 處與a
相交,並且c
(您的list_3
)在索引 9、11、12、15 和 18 處與a
相交。
您似乎希望返回的值以某種方式在不同線的交點之間交替,並“等待其他數組值穿過列表,然后它才能通過 go”。 尚不完全清楚這在每種情況下意味着什么,但您可以通過像這樣操作結果來做到這一點:
ip = intersection_points(a, b, c)
print(np.concatenate(([ip[0]], ip[1:][ip[:-1, 0] != ip[1:, 0]])))
返回
[[ 0, 7],
[ 1, 9],
[ 0, 13],
[ 1, 15]]
即第一個交叉點是索引 7 處的b
,然后是索引 9 處的c
,然后是 13 處的b
,最后是 15 處的c
。
此 function 將 arrays 轉換為列表並返回元組列表:
def find_cross(list_, list_2, list_3):
list_ = list_.tolist()
list_2 = list_2.tolist()
list_3 = list_3.tolist()
cross = []
i=0
for x,y in zip(list_2, list_3):
if x in list_:
cross.append((i, list_.index(x)))
if y in list_:
cross.append((i, list_.index(y)))
i+=1
return cross
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.