2020年12月31日星期四

numpy - explanation of the 3rd string integer of numpy.r_['string integer', array]

Question

Please help understand in layman's words what the 3rd string integer in numpy.r_['1,2,0', array] is and how it works.

numpy documentation says about the 3rd integer as below, but cannot figure out what it is exactly.

which axis should contain the start of the arrays which are less than the specified number of dimensions. In other words the third integer allows you to specify where the 1's should be placed in the shape of the arrays that have their shapes upgraded.

numpy.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).

A string with three comma-separated integers allows specification of the axis to concatenate along, the minimum number of dimensions to force the entries to, and which axis should contain the start of the arrays which are less than the specified number of dimensions. In other words the third integer allows you to specify where the 1's should be placed in the shape of the arrays that have their shapes upgraded. By default, they are placed in the front of the shape tuple. The third argument allows you to specify where 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 integers specify where in the new shape tuple the last dimension of upgraded arrays should be placed, so the default is '-1'.

Without the 3rd integer, the behavior makes sense. Frame a 2D shape and place the array elements along the 1st axis or 2nd axis.

print(np.r_['0,2', [1,2,3], [4,5,6]])  print(np.r_['1,2', [1,2,3], [4,5,6]])  ---  [[1 2 3]   [4 5 6]]  [[1 2 3 4 5 6]]  

However, not sure how the 3rd integer is converting the shape. It appears '1,2,0' is a transpose of '0,2,1', and '1,2,1' is that of '0,2,0' but no idea how this occurs.

print(np.r_['0,2', [1,2,3], [4,5,6]])  print()  print(np.r_['0,2,0', [1,2,3], [4,5,6]])  print(np.r_['0,2,1', [1,2,3], [4,5,6]])  ---  [[1 2 3]   [4 5 6]]    [[1]   [2]   [3]   [4]   [5]   [6]]  [[1 2 3]   [4 5 6]]  
print(np.r_['1,2', [1,2,3], [4,5,6]])  print()  print(np.r_['1,2,0', [1,2,3], [4,5,6]])  print(np.r_['1,2,1', [1,2,3], [4,5,6]])  ---  [[1 2 3 4 5 6]]    [[1 4]   [2 5]   [3 6]]  [[1 2 3 4 5 6]]  

The answer in Understanding the syntax of numpy.r_() concatenation is touching on the part, but not sure what it is telling completely. It seems like a switch to make the shape (1, 3) or its transpose (3, 1) when doing np.newaxis.

np.r_['0,2,-1', [1,2,3], [[4,5,6]]] has shape (3,) and shape (1, 3). np.r_ needs to add a 1 to its shape tuple to make it either (1,3) or (3,1). That is where the -1 in 0,2,-1 comes into play. It basically decides where the extra 1 needs to be placed in the shape tuple of the array.

https://stackoverflow.com/questions/65526672/numpy-explanation-of-the-3rd-string-integer-of-numpy-r-string-integer-arra January 01, 2021 at 09:12AM

没有评论:

发表评论