numpy.r_#
- numpy.r_=<numpy.lib._index_tricks_impl.RClassobject>#
Translates slice objects to concatenation along the first axis.
This is a simple way to build up arrays quickly. There are two use cases.
If the index expression contains comma separated arrays, then stackthem along their first axis.
If the index expression contains slice notation or scalars then createa 1-D array with a range indicated by the slice notation.
If slice notation is used, the syntax
start:stop:stepis equivalenttonp.arange(start,stop,step)inside of the brackets. However, ifstepis an imaginary number (i.e. 100j) then its integer portion isinterpreted as a number-of-points desired and the start and stop areinclusive. In other wordsstart:stop:stepjis interpreted asnp.linspace(start,stop,step,endpoint=1)inside of the brackets.After expansion of slice notation, all comma separated sequences areconcatenated together.Optional character strings placed as the first element of the indexexpression can be used to change the output. The strings ‘r’ or ‘c’ resultin matrix output. If the result is 1-D and ‘r’ is specified a 1 x N (row)matrix is produced. If the result is 1-D and ‘c’ is specified, then a N x 1(column) matrix is produced. If the result is 2-D then both provide thesame matrix result.
A string integer specifies which axis to stack multiple comma separatedarrays along. A string of two comma-separated integers allows indicationof the minimum number of dimensions to force each entry into as thesecond integer (the axis to concatenate along is still the first integer).
A string with three comma-separated integers allows specification of theaxis to concatenate along, the minimum number of dimensions to force theentries to, and which axis should contain the start of the arrays whichare less than the specified number of dimensions. In other words the thirdinteger allows you to specify where the 1’s should be placed in the shapeof the arrays that have their shapes upgraded. By default, they are placedin the front of the shape tuple. The third argument allows you to specifywhere the start of the array should be instead. Thus, a third argument of‘0’ would place the 1’s at the end of the array shape. Negative integersspecify where in the new shape tuple the last dimension of upgraded arraysshould be placed, so the default is ‘-1’.
- Parameters:
- Not a function, so takes no parameters
- Returns:
- A concatenated ndarray or matrix.
See also
concatenateJoin a sequence of arrays along an existing axis.
c_Translates slice objects to concatenation along the second axis.
Examples
>>>importnumpyasnp>>>np.r_[np.array([1,2,3]),0,0,np.array([4,5,6])]array([1, 2, 3, ..., 4, 5, 6])>>>np.r_[-1:1:6j,[0]*3,5,6]array([-1. , -0.6, -0.2, 0.2, 0.6, 1. , 0. , 0. , 0. , 5. , 6. ])
String integers specify the axis to concatenate along or the minimumnumber of dimensions to force entries into.
>>>a=np.array([[0,1,2],[3,4,5]])>>>np.r_['-1',a,a]# concatenate along last axisarray([[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5]])>>>np.r_['0,2',[1,2,3],[4,5,6]]# concatenate along first axis, dim>=2array([[1, 2, 3], [4, 5, 6]])
>>>np.r_['0,2,0',[1,2,3],[4,5,6]]array([[1], [2], [3], [4], [5], [6]])>>>np.r_['1,2,0',[1,2,3],[4,5,6]]array([[1, 4], [2, 5], [3, 6]])
Using ‘r’ or ‘c’ as a first string argument creates a matrix.
>>>np.r_['r',[1,2,3],[4,5,6]]matrix([[1, 2, 3, 4, 5, 6]])