查找3个Numpy数组Python的交点 [英] Finding the point of intersection of 3 Numpy Arrays Python
问题描述
我正在尝试编写一个函数,该函数可以为我提供 list_2
或 list_3
与 list _
交叉的索引.因此,如果在numpy代码中有任何交叉点,它将给我交叉点.我想按顺序获得叉号,因此必须对索引列表进行格式化,以便按 list_2叉,list_3叉,list_2叉
或 list_3叉的顺序给我叉,list_2交叉,list_3交叉
等.因此,如果发生交叉,则必须等待其他数组值穿过 list
才能通过.我不知道如何解决这个问题,尽管我尝试使用 numpy.where()
函数,例如,我也在使用pandas模块,所以如果它具有有效的功能,我也可以使用它.
I am trying to code a function where it gives me the indexes of where either list_2
or list_3
crosses list_
. So it would give me the points of intersection if there are any in numpy code. I want to get the crosses in order, so the list of indexes have to be formatted so that it would give me a cross in order of list_2 cross, list_3 cross , list_2 cross
or list_3 cross, list_2 cross , list_3 cross
etc. So if a cross has happened it has to wait for the other array values to cross the list
before it can go through. 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])
情节:
预期输出:
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
其中:
The crossing points or intersection indices between two series a
and b
are the indices i
where:
- (a i < b i 和a i + 1 > b i + 1 )(
b
从上方穿过a
) - or(a i > b i 和a i + 1 < b i + 1 )(
b
从下面穿过a
) - 或a i = b i (
a
和b
触摸)
- either (ai < bi and ai+1 > bi+1) (
b
crossesa
from above) - or (ai > bi and ai+1 < bi+1) (
b
crossesa
from below) - or ai = bi (
a
andb
touch)
因此,我们可以通过比较每个数组的'current'( i -th)和'next'( i + 1 -th)值来获取索引.
So we can get the indices by comparing the 'current' (i-th) and 'next' (i+1-th) values of each array.
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))
这将以以下格式返回交点数组:
This returns an array of intersection points in this format:
[[ 0 7]
[ 0 9]
[ 1 9]
[ 1 11]
[ 1 12]
[ 0 13]
[ 1 15]
[ 1 18]]
表示 b
(您的 list_2
)在索引7、9、13和 c
a 相交>(您的 list_3
)在索引9、11、12、15和18处与 a
相交.
meaning that b
(your list_2
) intersects with a
at indices 7, 9, 13, and c
(your list_3
) intersects with a
at indices 9, 11, 12, 15 and 18.
您似乎希望返回的值以某种方式在不同线的交点之间交替,并且等待其他数组值在通过之前先穿过列表".尚不清楚在每种情况下这意味着什么,但是您可以通过操纵如下结果来做到这一点:
You seem to want the returned value to somehow alternate between intersections of the different lines and 'wait for the other array values to cross the list before it can go through'. It's not entirely clear what this would mean in every case but you can possibly do this by manipulating the result like this:
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
,最后是 c
在15.
i.e. the first crossing is of b
at index 7, then c
at 9, then b
at 13, and finally c
at 15.
这篇关于查找3个Numpy数组Python的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!