What does np.r_ do (numpy)?

前端 未结 2 460
花落未央
花落未央 2021-01-31 14:57

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(bin         


        
2条回答
  •  情歌与酒
    2021-01-31 15:44

    numpy.r_[array[], array[]]
    

    This is used to concatenate any number of array slices along row (first) axis. This is a simple way to create numpy arrays quickly and efficiently.

    For instance, to create an array from two different arrays by selecting the elements of your choice, we'll have to assign the sliced values to a new varaible and use concatenation method to join them along an axis.

    >>> a = np.arange(9).reshape(3,3)
    >>> b = np.arange(10,19).reshape(3,3)
    >>> a
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    >>> b
    array([[10, 11, 12],
           [13, 14, 15],
           [16, 17, 18]])
    

    I want to create a new 2-D array, with 2*2 elements ([4,5,14,15]) then, I'll have to do the following,

    >>> slided_a = a[1,1:3]
    >>> sliced_b = b[1,1:3]
    >>> new_array = np.concatenate((sliced_a, sliced_b), axis = 0) 
    

    As this is clearly an inefficient way because, as the number of elements that are to be included in the new array increases, the temporary variables that are assigned to store the sliced values increases.

    This is where we use np.r_

    >>> c = np.r_[a[1,1:3],b[1,1:3]]
    array([ 4,  5, 14, 15])
    

    Likewise, if we want to create a new array by stacking the sliced values in 2nd axis, we can use np.c_

    >>> c = np.c_[a[1,1:3],b[1,1:3]]
    array([[ 4, 14],
           [ 5, 15]])
    

提交回复
热议问题