In [1]:
import numpy as np

In [2]:
a = np.arange(6).reshape(2, 3)
print(a)


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

In [3]:
print(np.expand_dims(a, 0))


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

In [4]:
print(np.expand_dims(a, 0).shape)


(1, 2, 3)

In [5]:
print(np.expand_dims(a, 1).shape)


(2, 1, 3)

In [6]:
print(np.expand_dims(a, 2).shape)


(2, 3, 1)

In [7]:
print(np.expand_dims(a, -1).shape)


(2, 3, 1)

In [8]:
print(np.expand_dims(a, -2).shape)


(2, 1, 3)

In [9]:
print(np.expand_dims(a, -3).shape)


(1, 2, 3)

In [10]:
print(np.expand_dims(a, 3).shape)


(2, 3, 1)
/usr/local/lib/python3.7/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.

In [11]:
print(np.expand_dims(a, -4).shape)


(2, 1, 3)
/usr/local/lib/python3.7/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.

In [12]:
# print(np.expand_dims(a, (0, 1)).shape)
# TypeError: '>' not supported between instances of 'tuple' and 'int'

In [13]:
a_expand_dims = np.expand_dims(a, 0)

In [14]:
print(np.shares_memory(a, a_expand_dims))


True