{:title ["Transposing and Axes Reordering"]}

Index

Transposing and Reshaping NDArrays

Transposing

In [1]:
import numpy as np

Axes of NDArrays

Q: How many indices do we need to address a single entry in a ndarray?

A: The number of axes the ndarray has.

In [2]:
# 2D matrix has 2 axes

M = np.array([[1,2,3],
              [4,5,6]])

M[0,1]
Out[2]:
2
In [3]:
# Listing the sizes of each axes
M.shape
Out[3]:
(2, 3)
In [4]:
# Explicitly counting the number of axes of M
len(M.shape)
Out[4]:
2
In [5]:
# Vector has 1 axes

v = np.array([1,2,3])
v[1]
Out[5]:
2
In [6]:
v.shape
Out[6]:
(3,)
In [7]:
len(v.shape)
Out[7]:
1
In [8]:
# Scalars have 0 axes
x = np.array(3.14)
x
Out[8]:
array(3.14)
In [9]:
x.shape
Out[9]:
()
In [10]:
len(x.shape)
Out[10]:
0

Transposing

Transpose is an operation in linear algebra. It makes the rows of input matrix into columns of the output matrix.

In [11]:
# Here is the input matrix
M
Out[11]:
array([[1, 2, 3],
       [4, 5, 6]])
In [12]:
# A quick way to obtain the transpose of M
M.T
Out[12]:
array([[1, 4],
       [2, 5],
       [3, 6]])
In [13]:
#
# M.T is a special case of more general multi-axes transpose.
#
np.transpose(M, (1, 0))
Out[13]:
array([[1, 4],
       [2, 5],
       [3, 6]])
In [14]:
#
# Transpose is also a method of the ndarray object
#
M.transpose((1,0))
Out[14]:
array([[1, 4],
       [2, 5],
       [3, 6]])

Reshaping NDArrays

We can change the shape of NDarrays as long as the total number of entries remain the same.

A ndarray with shape (4,3) has 12 elements. It can be reshaped into one (2,2,3)

+---+---+---+        +-------------------------------------+
|   |   |   |        |                                     |
+---+---+---+        |   +---+---+---+    +---+---+---+    |
|   |   |   |   ==>  |   |.  |.  |.  |    |.  |.  |.  |    |
+---+---+---+        |   +---+---+---+    +---+---+---+    |
|   |   |   |        |   |.  |.  |.  |    |.  |.  |.  |    |
+---+---+---+        |   +---+---+---+    +---+---+---+    |
|   |   |   |        |                                     |
+---+---+---+        +-------------------------------------+

NDArray to vectors

It's easier to see how ndarrays are reshaped into vectors.

In [15]:
M
Out[15]:
array([[1, 2, 3],
       [4, 5, 6]])
In [16]:
#
# Reshape always iterates over the __inner__ axes first,
# and then __outter__ axes.
#
np.reshape(M, (6,))
Out[16]:
array([1, 2, 3, 4, 5, 6])
In [17]:
#
# This is what numpy is doing (fast)
#
I,J = M.shape
for i in range(I):
    for j in range(J):
        print(M[i,j])
1
2
3
4
5
6
In [18]:
#
# We can also use the reshape method
#
M.reshape((6,))
Out[18]:
array([1, 2, 3, 4, 5, 6])

Vectors to NDArray

The reverse happens when converting a vector to a multi-axes ndarray.

In [19]:
v = np.array([1,2,3,4,5,6,7,8])
v
Out[19]:
array([1, 2, 3, 4, 5, 6, 7, 8])
In [20]:
v.reshape((2,4))
Out[20]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

NDArray to NDArray

A good way of understanding how reshape works is to imagine that the input ndarray is converted to a vector, which in turn is converted to the output ndarray.

In [21]:
A = np.array([[[1,2],
               [3,4]],
              [[5,6],
               [7,8]]])
A
Out[21]:
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
In [22]:
A.shape
Out[22]:
(2, 2, 2)
In [23]:
B = A.reshape(2,4)
B
Out[23]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

Transposing vs Reshaping

Transposing and reshaping are not the same. They may change the shape of a ndarray in the same way, but:

  1. Transpose changes the order of the axes.
  2. Reorder changes the dimensions of each axis.
In [24]:
M
Out[24]:
array([[1, 2, 3],
       [4, 5, 6]])
In [25]:
M.transpose(1,0)
Out[25]:
array([[1, 4],
       [2, 5],
       [3, 6]])
In [26]:
M.reshape(3, 2)
Out[26]:
array([[1, 2],
       [3, 4],
       [5, 6]])