``````

In [1]:

import numpy as np

``````
``````

In [2]:

a_2d = np.arange(6).reshape(2, 3)
print(a_2d)

``````
``````

[[0 1 2]
[3 4 5]]

``````
``````

In [3]:

a_2d_T = a_2d.T
print(a_2d_T)

``````
``````

[[0 3]
[1 4]
[2 5]]

``````
``````

In [4]:

print(np.shares_memory(a_2d, a_2d_T))

``````
``````

True

``````
``````

In [5]:

a_2d_T[0, 1] = 100
print(a_2d_T)

``````
``````

[[  0 100]
[  1   4]
[  2   5]]

``````
``````

In [6]:

print(a_2d)

``````
``````

[[  0   1   2]
[100   4   5]]

``````
``````

In [7]:

a_2d[1, 0] = 3
print(a_2d)

``````
``````

[[0 1 2]
[3 4 5]]

``````
``````

In [8]:

print(a_2d_T)

``````
``````

[[0 3]
[1 4]
[2 5]]

``````
``````

In [9]:

a_2d_T_copy = a_2d.T.copy()
print(a_2d_T_copy)

``````
``````

[[0 3]
[1 4]
[2 5]]

``````
``````

In [10]:

print(np.shares_memory(a_2d, a_2d_T_copy))

``````
``````

False

``````
``````

In [11]:

a_2d_T_copy[0, 1] = 100
print(a_2d_T_copy)

``````
``````

[[  0 100]
[  1   4]
[  2   5]]

``````
``````

In [12]:

print(a_2d)

``````
``````

[[0 1 2]
[3 4 5]]

``````
``````

In [13]:

a_2d = np.arange(6).reshape(2, 3)
print(a_2d)

``````
``````

[[0 1 2]
[3 4 5]]

``````
``````

In [14]:

print(a_2d.transpose())

``````
``````

[[0 3]
[1 4]
[2 5]]

``````
``````

In [15]:

print(np.shares_memory(a_2d, a_2d.transpose()))

``````
``````

True

``````
``````

In [16]:

print(np.transpose(a_2d))

``````
``````

[[0 3]
[1 4]
[2 5]]

``````
``````

In [17]:

print(np.shares_memory(a_2d, np.transpose(a_2d)))

``````
``````

True

``````
``````

In [18]:

a_1d = np.arange(3)
print(a_1d)

``````
``````

[0 1 2]

``````
``````

In [19]:

print(a_1d.T)

``````
``````

[0 1 2]

``````
``````

In [20]:

print(a_1d.transpose())

``````
``````

[0 1 2]

``````
``````

In [21]:

print(np.transpose(a_1d))

``````
``````

[0 1 2]

``````
``````

In [22]:

a_row = a_1d.reshape(1, -1)
print(a_row)

``````
``````

[[0 1 2]]

``````
``````

In [23]:

print(a_row.shape)

``````
``````

(1, 3)

``````
``````

In [24]:

print(a_row.ndim)

``````
``````

2

``````
``````

In [25]:

a_col = a_1d.reshape(-1, 1)
print(a_col)

``````
``````

[[0]
[1]
[2]]

``````
``````

In [26]:

print(a_col.shape)

``````
``````

(3, 1)

``````
``````

In [27]:

print(a_col.ndim)

``````
``````

2

``````
``````

In [28]:

print(a_row.T)

``````
``````

[[0]
[1]
[2]]

``````
``````

In [29]:

print(a_col.T)

``````
``````

[[0 1 2]]

``````
``````

In [30]:

a_3d = np.arange(24).reshape(2, 3, 4)
print(a_3d)

``````
``````

[[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

``````
``````

In [31]:

print(a_3d.T)

``````
``````

[[[ 0 12]
[ 4 16]
[ 8 20]]

[[ 1 13]
[ 5 17]
[ 9 21]]

[[ 2 14]
[ 6 18]
[10 22]]

[[ 3 15]
[ 7 19]
[11 23]]]

``````
``````

In [32]:

print(a_3d.T.shape)

``````
``````

(4, 3, 2)

``````
``````

In [33]:

print(a_3d.transpose())

``````
``````

[[[ 0 12]
[ 4 16]
[ 8 20]]

[[ 1 13]
[ 5 17]
[ 9 21]]

[[ 2 14]
[ 6 18]
[10 22]]

[[ 3 15]
[ 7 19]
[11 23]]]

``````
``````

In [34]:

print(a_3d.transpose().shape)

``````
``````

(4, 3, 2)

``````
``````

In [35]:

print(a_3d.transpose(2, 1, 0))

``````
``````

[[[ 0 12]
[ 4 16]
[ 8 20]]

[[ 1 13]
[ 5 17]
[ 9 21]]

[[ 2 14]
[ 6 18]
[10 22]]

[[ 3 15]
[ 7 19]
[11 23]]]

``````
``````

In [36]:

print(a_3d.transpose(2, 1, 0).shape)

``````
``````

(4, 3, 2)

``````
``````

In [37]:

print(a_3d.transpose((2, 1, 0)).shape)

``````
``````

(4, 3, 2)

``````
``````

In [38]:

print(np.transpose(a_3d, (2, 1, 0)))

``````
``````

[[[ 0 12]
[ 4 16]
[ 8 20]]

[[ 1 13]
[ 5 17]
[ 9 21]]

[[ 2 14]
[ 6 18]
[10 22]]

[[ 3 15]
[ 7 19]
[11 23]]]

``````
``````

In [39]:

print(np.transpose(a_3d, (2, 1, 0)).shape)

``````
``````

(4, 3, 2)

``````
``````

In [40]:

# print(np.transpose(a_3d, 2, 1, 0))
# TypeError: transpose() takes from 1 to 2 positional arguments but 4 were given

``````
``````

In [41]:

# print(a_3d.transpose(0, 1))
# ValueError: axes don't match array

``````
``````

In [42]:

# print(a_3d.transpose(0, 1, 2, 3))
# ValueError: axes don't match array

``````
``````

In [43]:

# print(a_3d.transpose(0, 1, 3))
# AxisError: axis 3 is out of bounds for array of dimension 3

``````
``````

In [44]:

print(a_3d)

``````
``````

[[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

``````
``````

In [45]:

print(a_3d.shape)

``````
``````

(2, 3, 4)

``````
``````

In [46]:

print(a_3d.transpose(0, 2, 1))

``````
``````

[[[ 0  4  8]
[ 1  5  9]
[ 2  6 10]
[ 3  7 11]]

[[12 16 20]
[13 17 21]
[14 18 22]
[15 19 23]]]

``````
``````

In [47]:

print(a_3d.transpose(0, 2, 1).shape)

``````
``````

(2, 4, 3)

``````
``````

In [48]:

print(a_3d.transpose(1, 0, 2))

``````
``````

[[[ 0  1  2  3]
[12 13 14 15]]

[[ 4  5  6  7]
[16 17 18 19]]

[[ 8  9 10 11]
[20 21 22 23]]]

``````
``````

In [49]:

print(a_3d.transpose(1, 0, 2).shape)

``````
``````

(3, 2, 4)

``````
``````

In [50]:

print(a_3d[:, :, 0])

``````
``````

[[ 0  4  8]
[12 16 20]]

``````
``````

In [51]:

print(a_3d.transpose(1, 0, 2)[:, :, 0])

``````
``````

[[ 0 12]
[ 4 16]
[ 8 20]]

``````