Numpy.transpose of three-dimensional array with the method of transpose

Source: Internet
Author: User
The following for you to share a numpy.transpose on the three-dimensional array of the Transpose method, has a good reference value, I hope to be helpful to everyone. Come and see it together.

As shown below:

Import NumPy as NP

Three-dimensional arrays

ARR1 = Np.arange (+). Reshape ((2, 2, 4) #[[[0 1 2 3] # [4 5 6 7]]  # [[8 9] [[] [] []]  arr2=arr1.tr Anspose ((1,0,2)) #[[[0 1 2 3] # [8 9 10 11] # # [[4 5 6 7] # [12 13 14 15]]

The positive order is (0,1,2) and the array is

#[[[0 1 2 3] # [4 5 6 7]]  # [[8 9 10 11] # [12 13 14 15]]

Why did you go to Tanspose (1,0,2) and the array becomes

#[[[0 1 2 3] # [8 9 10 11]] # # [[4 5 6 7] # [12 13 14 15]]

After careful observation, you can see the difference between the transpose array and the pre-transpose array is that the second line of the first page and the first line of the second page are swapped, but why?

When I use arr1[0,1,0], the index value is 4

When I use arr2[1,0,0], the index value is 4

There seems to be some connection between the change of the index parameter table and the difference between the positive sequence and the transpose order.

For arr1 arrays, the index parameter table [0,0,X] can represent the first row of the first page, the index parameter table of the same element does not change after the current two parameters are swapped

So the first page of ARR2 first line and the first page of arr1 are the same

For arr1 arrays, the index parameter table [0,1,X] can represent the second row of the first page, after the current two parameters are swapped, the index value of the same element such as [0,1,0] becomes [1,0,0],

This is the difference between the index parameter table that explains the index value 4

That's probably the idea. So transpose (1,0,2), the first page of the array the second row and the second page of the first line swap

The following four kinds of transpose way is also roughly this idea, careful observation, it should not be difficult to understand

Arr3=arr1.transpose (0,2,1) # [[[  0 4] # [1 5] # [2 6] # [3 7]] # # [[8] # [9] # [Ten] # [all]]  a Rr4=arr1.transpose ((2,0,1)) #[[[0 4] # [8 12]] # # [[1 5] # [9 13]] # # [[2 6] # [10 14]] # # [[3 7] # [11 15]]

It is important to note that the ARR4 array becomes 4 pages, because after the page number and line code are swapped,

Page number from 2 to 4

and the line code from the number 4, became 2

Arr5=arr1.transpose ((2,1,0)) #[[[0 8] # [4 12]] # # [[1 9] # [5 13]] # # [[2 10] # [6 14]] # # [[3 11] # [7 15]]]< C4/>arr6=arr1.transpose ((1,2,0)) #[[[0 8] # [1 9] # [2 10] # [3 11]] # # [[4 12] # [5 13] # [6 14] # [7 15]]

In addition, transpose (2,0,1) can be seen as the first transpose (0,2,1) and then transpose (1,0,2)

Transpose (2,1,0) can be seen as, first transpose (1,0,2), then transpose (0,2,1), and finally transpose (1,0,2)

Transpose (1,2,0) can be seen as, first transpose (1,0,2), in transpose (0,2,1)

The code can be written

Arr4=arr1.transpose (0,2,1). Transpose (1,0,2)

#[[[0 4]# [8 12]]## [[1 5]#] [9 13]]## [[2 6]#] [10 14]]## [[3 7]# [11 15]]

The same result!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.