Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_?

like image 487
Philipp Avatar asked Jun 02 '15 13:06

Philipp


People also ask

What is NP c_ in Python?

numpy. c_ = <numpy.lib.index_tricks.CClass object> Translates slice objects to concatenation along the second axis. This is short-hand for np. r_['-1,2,0', index expression] , which is useful because of its common occurrence.

What does NP Columnstack do?

NumPy: column_stack() functionTake a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack.

What does NP Argwhere return?

The np. argwhere() is a Numpy library function used to find the indices of array elements that are nonzero, grouped by element. The numpy argwhere() function takes an array-like parameter and returns the indices of the array elements.


2 Answers

What it does is 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.

like image 190
omerbp Avatar answered Sep 23 '22 03:09

omerbp


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]]) 
like image 26
vivek Avatar answered Sep 21 '22 03:09

vivek