微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找到3个Numpy数组Python的交点

如何解决找到3个Numpy数组Python的交点

我正在尝试编写一个函数,它为我提供了 list_2list_3 交叉 list_ 位置的索引。因此,如果 numpy 代码中有任何交点,它会给我交点。我想按顺序排列交叉,因此必须对索引列表进行格式化,以便它按照 list_2 cross,list_3 cross,list_2 crosslist_3 cross,list_2 cross,list_3 cross 等的顺序给我一个交叉。因此,如果发生了交叉,它有在它可以通过之前等待其他数组值穿过 list。我不知道如何解决这个问题,尽管我尝试过使用 numpy.where() 函数等,但我也在使用 pandas 模块,所以如果它有一个有效的函数,我也可以使用它。>

变量:

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])

情节:

enter image description here

预期输出

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

解决方法

两个系列 ab 之间的交叉点或交叉点索引是索引 i 其中:

  • 要么 (aii 和 ai+1 > bi+1) ( b 从上方穿过 a)
  • or (ai > bi and ai+1i+1) ( b 从下方穿过 a
  • 或 ai = biab 触摸)

因此我们可以通过比较每个数组的“当前”(i-th) 和“下一个”(i+1-th) 值来获得索引。

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,9910.23,9920.79,9911.34,9920.01,9927.51,9932.3,9932.33,9928.87,9929.22,9935.24,9935.26,9935.68,9940.5])
b = np.array([9935.26,9940.5,9925.19,9929.62,9929.65,9929.93,9932.55,9936.81,9936.84,9937.26,9932.6,9932.6])
c = np.array([9928.87,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)与{相交{1}} 在索引 9、11、12、15 和 18 处。

您似乎希望返回的值以某种方式在不同线的交叉点之间交替,并且“等待其他数组值在它可以通过之前穿过列表”。在每种情况下这意味着什么并不完全清楚,但您可以通过像这样操作结果来做到这一点:

a

回来

ip = intersection_points(a,c)
print(np.concatenate(([ip[0]],ip[1:][ip[:-1,0] != ip[1:,0]])))

即第一个交叉点是索引 7 处的 [[ 0,7],[ 1,9],[ 0,13],15]] ,然后是 9 处的 b,然后是 13 处的 c,最后是 15 处的 b

,

此函数将数组转换为列表并返回元组列表:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。