pythonnumpymerge

What does np.r_ do (numpy)?


Following code is taken from numpy function base on github

sa = sort(a[i:i+block])
n += np.r_[sa.searchsorted(bins[:-1], 'left'),
           sa.searchsorted(bins[-1], 'right')]

So I know that searchsorted finds the position in the array sa where the elements of bins would have to be inserted in order to keep sa sorted (left gives the index left of where we would insert the value and right the right index). What I don't understand is the whole construction around it meaning what is

np.r_[array,array]

What is np.r_?


Solution

  • It does row-wise merging. This post has some nice example:

    >>> V = array([1,2,3,4,5,6])
    >>> Y = array([7,8,9,10,11,12])
    >>> np.r_[V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]]
    array([ 1,  2,  7,  4,  8,  9,  5,  6, 11, 12])
    

    Read more about it in this , and in the documentation of numpy.