1
want to understand the meaning of the tuple that the following code passed inside the command . transpose, I went after the documentation but it was not clear to me:
arr = np.arange(16). reshape((2,2,4))
arr.transpose((1,0,2))
1
want to understand the meaning of the tuple that the following code passed inside the command . transpose, I went after the documentation but it was not clear to me:
arr = np.arange(16). reshape((2,2,4))
arr.transpose((1,0,2))
3
You have to remember that the numpy.ndarray
not only for two-dimensional matrices, as we are used to, but for "multidimensional arrays" - so, if you have a two-dimensional matrix, and you don’t pass any parameters, it does the transposition as we’re used to.
If the parameter is passed, it indicates which axes will be exchanged with which - that is, in the example you passed, the position "0" indicates the axis zero - the value "1" passed there indicates that this axis of position 0 will be "reallocated" to axis "1". The value at position "1" is given the value "0", indicating that the axis "1" will become the axis "0". And finally, the "2" in the "2" position indicates that this axis will not be changed.
That is, in this specific example, if you think of the structure you created as 4 matrices 2 X 2 "stacked" -- voc}and are saying to transpose the first two axes: each of these 4 matrices 2 x 2, and keep the same order of the matrices.
Below I make another example where I create a structure that can be visualized more easily as "3 2x2 matrices", and ask to keep the first axis, and transpose the other 2 - result, in this visualization, each of the "2x2 matrices" appears transposed:
In [45]: a = np.arange(12).reshape((3, 2, 2))
In [46]: a
Out[46]:
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])
In [47]: b = np.transpose(a, (0, 2, 1))
In [48]: b
Out[48]:
array([[[ 0, 2],
[ 1, 3]],
[[ 4, 6],
[ 5, 7]],
[[ 8, 10],
[ 9, 11]]])
Browser other questions tagged python numpy
You are not signed in. Login or sign up in order to post.