Concatenate range arrays given start, stop numbers in a vectorized way - NumPy

后端 未结 1 896
南方客
南方客 2020-12-06 14:43

I have two matrices of interest, the first is a \"bag of words\" matrix, with two columns: the document ID and the term ID. For example:

bow[0:10]

Out[1]:
         


        
相关标签:
1条回答
  • 2020-12-06 15:10

    Think I have cracked it finally with a cumsum trick for a vectorized solution -

    def create_ranges(a):
        l = a[:,1] - a[:,0]
        clens = l.cumsum()
        ids = np.ones(clens[-1],dtype=int)
        ids[0] = a[0,0]
        ids[clens[:-1]] = a[1:,0] - a[:-1,1]+1
        out = ids.cumsum()
        return out
    

    Sample runs -

    In [416]: a = np.array([[4,7],[10,16],[11,18]])
    
    In [417]: create_ranges(a)
    Out[417]: array([ 4,  5,  6, 10, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 16, 17])
    
    In [425]: a = np.array([[-2,4],[-5,2],[11,12]])
    
    In [426]: create_ranges(a)
    Out[426]: array([-2, -1,  0,  1,  2,  3, -5, -4, -3, -2, -1,  0,  1, 11])
    

    If we are given starts and stops as two 1D arrays, we just need to use those in place of the first and second columns. For completeness, here's the complete code -

    def create_ranges(starts, ends):
        l = ends - starts
        clens = l.cumsum()
        ids = np.ones(clens[-1],dtype=int)
        ids[0] = starts[0]
        ids[clens[:-1]] = starts[1:] - ends[:-1]+1
        out = ids.cumsum()
        return out
    
    0 讨论(0)
提交回复
热议问题