python - Numpy function to find indices for overlapping vectors -
it seems there should numpy function finding overlap of 2 vectors, can't seem find it. maybe 1 of knows it?
the problem best described simple code (below). have 2 sets of info (x1, y1), , (x2, y2), each x , y hundreds of elements. need truncate them domains same (i.e. x1 = x2), , y1 represents appropriate range go new x1, y2 truncated go new x2.
# x1 , y1 abscissa , ordinate measurement. x1 = array([1,2,3,4,5,6,7,8,9,10]) y1 = x1**2 # i'm making numbers ordinate. # x2 , y2 abscissa , ordinate different measurement, # not on same exact range. x2 = array([5,6,7,8,9,10,11,12,13]) y2 = sqrt(x2) # , more numbers aren't same. # , need math on portion 2 measurements overlap. x3 = array([5,6,7,8,9,10]) y3 = y1[4:10] + y2[:6] # there simple function give me these indices, # or have loops , compare values? print x1[4:10] print x2[:6] # ------------ next want replace ------------- # doing loops clumsy... # check vector starts lower. if x1[0] <= x2[0]: # loop through until find index matches start of other. in range(len(x1)): # here is. if x1[i] == x2[0]: # note offsets new starts of both vectors. x1off = x2off = 0 break else: in range(len(x2)): if x2[i] == x1[0]: x1off = 0 x2off = break # cutoff beginnings of vectors appropriate. x1 = x1[x1off:] y1 = y1[x1off:] x2 = x2[x2off:] y2 = y2[x2off:] # create lengths of vectors same. # see longer. if len(x1) > len(x2): # cutting off longer 1 same length shorter. x1 = x1[:len(x2)] y1 = y1[:len(x2)] elif len(x2) > len(x1): x2 = x2[:len(x1)] y2 = y2[:len(x1)] # ok, domains , ranges 2 (x,y) sets identical. print x1, y1 print x2, y2
thanks!
for simple intersection, can utilize 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])
but looks need different. @joranbeasley suggested in comment, can utilize np.in1d
, need utilize twice:
here's data:
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])
get subset of (x1, y1) data:
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]))
get subset of (x2, y2) data. note order of arguments 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]))
we didn't have form xx2
, because same xx1
. can operate on yy1
, yy2
. e.g.:
in [69]: yy1 + yy2 out[69]: array([ 27.23606798, 38.44948974, 51.64575131, 66.82842712, 84. , 103.16227766])
python numpy
No comments:
Post a Comment