Python Learning Notes (1)--numpy array transpose __python

Source: Internet
Author: User

About the transpose of the array, NumPy provides the transpose function and. T property two implementations, General transpose is more convenient to use, in addition to the conversion of the two axes can also be used swapreaxes , the following examples to do the introduction.

 #一维数组转置 >>> arr = np.arange (6) >>> print arr [0, 1, 2, 3, 4, 5] >>> PR  int Np.transpose (arr) [0, 1, 2, 3, 4, 5] #一维还是一维 ... #二维数组转置 >>> arr = np.arange (6). Reshape ((2,3)) >>> print arr [[0, 1, 2], [3, 4, 5]] >>> print np.transpose (arr) [[0,3], [1,4], [3,5]] #三维数组的转置 >>> arr = np.a  Range. Reshape (2,3,4) >>> print arr [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14,
 [0, 12], [4, 16], [8, 20]], >>> print np.transpose (arr) [[+]], [[+]], [[1], [5, [9]], [[2], [6], [d]], [[3], [7, d], [one,]]] #当数组 >= Three-dimensional, we may You want to transpose according to a specific rule, transpose can accept the tuple >>> print np.transpose (arr, (1,0,2)) for the axis number that specifies the transpose [[[0, 1, 2, 3], [12, 13, 14, 1 5]], [[4, 5, 6, 7], [[8, 9, A, one], [M, O,]] 

For whether to specify the conversion rules, the specific three-dimensional changes: The original data "three-dimensional" is (2,3,4), do not specify the conversion rules after the "three-dimensional" is (4,3,2), and the three-dimensional after the specified rule is according to the rules we specify, the two dimensions of its one and

#原始三维数据规则
>>> Print (Arr.shape)
(2, 3, 4)
#不指定转换规则
>>> print (Np.transpose (arr). Shape)
(4, 3, 2)
#指定转换规则
>>> print (Np.transpose (arr, (1, 0, 2)). Shape
(3, 2, 4)

The Ndarray t attribute is simpler to use, just to be followed by the array. T can. The T property is actually a special case of the transpose, that is, the default rule for the transpose rule is not specified.

#一维数组转置
>>> arr = np.arange (6)
>>> print arr
[0, 1, 2, 3, 4, 5]
>>> print arr.t
[0, 1, 2, 3, 4, 5] #一维还是一维 ...
#二维数组转置
>>> arr = np.arange (6). Reshape ((2,3))
>>> print arr
[[0, 1, 2],
 [3, 4, 5]]< c10/>>>> Print arr. T
[[0,3],
 [1,4],
 [3,5]]
#三维数组的转置
>>> arr = Np.arange ((reshape))
>>> print arr
[[0, 1, 2, 3],
   [4, 5, 6, 7],
   [8, 9, one]], [[A, M]
  ,
   [16 , A, M, [arr]]]
>>> print A. T [[[0], [4],
  [8, M]],
 [[1], [
  5, M],
  [9,]],
  [[2, 6] [[3]], [[7], [
  11, 23]]]

In some cases, you may only need to convert the two axes, except that you can use transpose to specify the axis (and, of course, each axis is specified by the way, just adjust the part of it), and you can also use swapreaxes.

>>> arr = np.arange. Reshape ((2,3,4)) >>> print arr [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, ten, one]] [[[A], [A, M], [A, Q,]]] >>> print arr. Swapaxes (1, 0) [[[0, 1, 2, 3], [12, 13, 14, 15]]], [[4, 5, 6, 7], [16, 17, 18, 19]], [[8, 9, 10, 11], [20, 21 )] 

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.