Adding dimensions to numpy.arrays: newaxis v.s. reshape v.s. expand_dims

Yang Zhang
4 min readDec 20, 2017

This post demonstrates 3 ways to add new dimensions to numpy.arrays using numpy.newaxis, reshape, or expand_dim. It covers these cases with examples:

  • 1.1 From 0-D (scalar) to n-D
  • 1.1.1 From 0-D to 1-D
  • 1.1.2 From 0-D to 2-D
  • 1.2 From 1-D to n-D (n>1)
  • 1.2.1 From 1-D to 2-D
  • 1.3 From 2-D to n-D (n>2)
  • 1.3.1 2-D to 3-D
  • 1.3.1.1 Case 1 : Add newaxis at the beginning
  • 1.3.1.2 Case 2: Squeeze newaxis in the middle
  • 1.3.1.3 Case 3: Add newaxis to the end
from numpy import array, newaxis, expand_dims

First define a display function.

def show_array(y):print('array: \n', y)print('array.ndim:', y.ndim)print('array.shape:', y.shape)

1.1 From 0-D (scalar) to n-D

x = array(5)show_array(x)array: 
5
array.ndim: 0
array.shape: ()

1.1.1 From 0-D to 1-D

y is the result of adding a new dimension to x, and the shape is no longer empty.

y = array(x)[newaxis]show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)

Equivalently,

y = expand_dims(x, axis=0)show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)

Any number >= 0 does the same as the above.

y = expand_dims(x, axis=123456)show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
"""Entry point for launching an IPython kernel.

Also equivalently,

y = x.reshape(-1,)show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)

1.1.2 From 0-D to 2-D

y = array(x)[newaxis, newaxis]show_array(y)array: 
[[5]]
array.ndim: 2
array.shape: (1, 1)
y = expand_dims(x, axis=0)z = expand_dims(y, axis=0)show_array(z)array:
[[5]]
array.ndim: 2
array.shape: (1, 1)
y = x.reshape(-1, 1)show_array(y)array:
[[5]]
array.ndim: 2
array.shape: (1, 1)

1.2 From 1-D to n-D (n>1)

x = array([5, 6, 7])show_array(x)array: 
[5 6 7]
array.ndim: 1
array.shape: (3,)

1.2.1 From 1-D to 2-D

Vector to row matrix

Adding newaxis at the beginning changes the shape from (3,) to (1, 3).

y = array(x)[newaxis, :]show_array(y)array: 
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = array(x)[newaxis] # This is short hand of y = array(x)[newaxis, :]show_array(y)array:
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = expand_dims(x, axis=0)show_array(y)array:
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = x.reshape(1, -1)show_array(y)array:
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)

Vector to column matrix

Adding newaxis at the end changes the shape from (3,) to (3, 1).

y = array(x)[:, newaxis]show_array(y)array: 
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)
y = expand_dims(x, axis=1)show_array(y)array:
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)

Equivalently,

y = expand_dims(x, axis=-1)show_array(y)array: 
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)

Any number >= 1 does the same.

y = expand_dims(x, axis=123456)show_array(y)array: 
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
"""Entry point for launching an IPython kernel.
y = x.reshape(-1, 1)show_array(y)array:
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)

1.3 From 2-D to n-D (n>2)

x = array([[1, 2, 3], [4, 5, 6]])show_array(x)array: 
[[1 2 3]
[4 5 6]]
array.ndim: 2
array.shape: (2, 3)

1.3.1 2-D to 3-D

1.3.1.1 Case 1 : Add newaxis at the beginning

y = array(x)[newaxis, :, :]show_array(y)array: 
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)

Equivalently,

y = array(x)[newaxis, :]show_array(y)array: 
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)

Equivalently,

y = array(x)[newaxis]show_array(y)array: 
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = expand_dims(x, axis=0)show_array(y)array:
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = x.reshape(-1, 2, 3)show_array(y)array:
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = x.reshape(-1, *x.shape)show_array(y)array:
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)

1.3.1.2 Case 2: Squeeze newaxis in the middle

y = array(x)[:, newaxis, :]show_array(y)array: 
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = array(x)[:, newaxis]show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = expand_dims(x, axis=1)show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = x.reshape(2, 1, 3)show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = x.reshape(x.shape[0], -1, x.shape[1])show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)

1.3.1.3 Case 3: Add newaxis to the end

y = array(x)[:, :, newaxis]show_array(y)array: 
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
y = expand_dims(x, axis=2)show_array(y)array:
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
y = expand_dims(x, axis=-1)show_array(y)array:
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)

Any number >= 2 does the same.

y = expand_dims(x, axis=123456)show_array(y)array: 
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
"""Entry point for launching an IPython kernel.
y = x.reshape(*x.shape, -1)show_array(y)array:
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)

Notebook is here: https://github.com/yang-zhang/yang-zhang.github.io/blob/master/ds_code/numpy_newaxis.ipynb

--

--