Numpy函数用于查找重叠向量的索引

时间:2014-06-19 23:25:18

标签: python numpy

似乎应该有一个numpy函数来找到两个向量的重叠,但我似乎无法找到它。也许你们其中一个人知道吗?

最好用简单的代码(下面)描述问题。我有两组数据(x1,y1)和(x2,y2),其中每个x和y是数百个元素。我需要截断所有域以使域相同(即x1 = x2),并且y1表示与新x1一起使用的适当范围,y2也被截断以与新x2一起使用。

# x1 and y1 are abscissa and ordinate from some measurement.
x1 = array([1,2,3,4,5,6,7,8,9,10])
y1 = x1**2 # I'm just making some numbers for the ordinate.

# x2 and y2 are abscissa and ordinate from a different measurement, 
# but not over the same exact range.
x2 = array([5,6,7,8,9,10,11,12,13])
y2 = sqrt(x2) # And some more numbers that aren't the same.

# And I need to do some math on just the portion where the two measurements overlap.
x3 = array([5,6,7,8,9,10])
y3 = y1[4:10] + y2[:6]

# Is there a simple function that would give me these indices, 
# or do I have to do loops and compare values?
print x1[4:10]
print x2[:6]


# ------------ THE FOLLOWING IS WHAT I WANT TO REPLACE -------------


# Doing loops is really clumsy...

# Check which vector starts lower.
if x1[0] <= x2[0]:
    # Loop through it until you find an index that matches the start of the other.
    for i in range(len(x1)):
        # Here is is.
        if x1[i] == x2[0]:
            # Note the offsets for the new starts of both vectors.
            x1off = i
            x2off = 0
            break
else:
    for i in range(len(x2)):
        if x2[i] == x1[0]:
            x1off = 0
            x2off = i
            break

# Cutoff the beginnings of the vectors as appropriate.
x1 = x1[x1off:]
y1 = y1[x1off:]
x2 = x2[x2off:]
y2 = y2[x2off:]

# Now make the lengths of the vectors be the same.
# See which is longer.
if len(x1) > len(x2):
    # Cut off the longer one to be the same length as the shorter.
    x1 = x1[:len(x2)]
    y1 = y1[:len(x2)]
elif len(x2) > len(x1):
    x2 = x2[:len(x1)]
    y2 = y2[:len(x1)]

# OK, now the domains and ranges for the two (x,y) sets are identical.    
print x1, y1
print x2, y2

谢谢!

1 个答案:

答案 0 :(得分:3)

对于简单的交叉点,您可以使用np.intersect1d

In [20]: x1 = array([1,2,3,4,5,6,7,8,9,10])

In [21]: x2 = array([5,6,7,8,9,10,11,12,13])

In [22]: x3 = np.intersect1d(x1, x2)

In [23]: x3
Out[23]: array([ 5,  6,  7,  8,  9, 10])

但看起来你需要不同的东西。正如@JoranBeasley在评论中建议的那样,你可以使用np.in1d,但你需要使用它两次:

这是数据:

In [57]: x1
Out[57]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

In [58]: y1
Out[58]: array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100])

In [59]: x2
Out[59]: array([ 5,  6,  7,  8,  9, 10, 11, 12, 13])

In [60]: y2
Out[60]: 
array([ 2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ,
        3.16227766,  3.31662479,  3.46410162,  3.60555128])

获取(x1,y1)数据的子集:

In [61]: mask1 = np.in1d(x1, x2)

In [62]: xx1 = x1[mask1]

In [63]: yy1 = y1[mask1]

In [64]: xx1, yy1
Out[64]: (array([ 5,  6,  7,  8,  9, 10]), array([ 25,  36,  49,  64,  81, 100]))

获取(x2,y2)数据的子集。请注意,np.in1d的参数顺序现为x2, x1

In [65]: mask2 = np.in1d(x2, x1)

In [66]: xx2 = x2[mask2]

In [67]: yy2 = y2[mask2]

In [68]: xx2, yy2
Out[68]: 
(array([ 5,  6,  7,  8,  9, 10]),
 array([ 2.23606798,  2.44948974,  2.64575131,  2.82842712,  3.        ,
         3.16227766]))

我们确实不必形成xx2,因为它与xx1相同。我们现在可以在yy1yy2上操作。 E.g:

In [69]: yy1 + yy2
Out[69]: 
array([  27.23606798,   38.44948974,   51.64575131,   66.82842712,
         84.        ,  103.16227766])