\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)

Removing length 1 axes with numpy.squeezeΒΆ

Sometimes we find ourselves with arrays with length-1 axes - and we want to remove these axes. For example:

>>> import numpy as np
>>> arr = np.random.normal(size=(4, 1, 6))
>>> arr.shape
(4, 1, 6)
>>> squeezed = np.squeeze(arr)
>>> squeezed.shape
(4, 6)
>>> arr = np.zeros((1, 3, 1, 7))
>>> arr.shape
(1, 3, 1, 7)
>>> np.squeeze(arr).shape
(3, 7)