I read the following in the numpy documentation for the function r_:
A string integer specifies which axis to stack multiple comma
separated arrays along. A string of two comma-separated integers
allows indication of the minimum number of dimensions to force each
entry into as the second integer (the axis to concatenate along is
still the first integer).
and they give this example:
>>> np.r_['0,2', [1,2,3], [4,5,6]] # concatenate along first axis, dim>=2
array([[1, 2, 3],
[4, 5, 6]])
I don’t follow, what does exactly the string '0,2' instruct numpy to do?
Other than the link above, is there another site with more documentation about this function?
'n,m'tellsr_to concatenate alongaxis=n, and produce a shape with at leastmdimensions:So we are concatenating along axis=0, and we would normally therefore expect the result to have shape
(6,), but sincem=2, we are tellingr_that the shape must be at least 2-dimensional. So instead we get shape(2,3):Look at what happens when we increase
m:Anything you can do with
r_can also be done with one of the more readable array-building functions such asnp.concatenate,np.row_stack,np.column_stack,np.hstack,np.vstackornp.dstack, though it may also require a call toreshape.Even with the call to reshape, those other functions may even be faster: