``````

In [1]:

import numpy as np

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

In [2]:

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

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

[[[[[0]
[1]
[2]]]

[[[3]
[4]
[5]]]]]

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

In [3]:

print(a.shape)

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

(1, 2, 1, 3, 1)

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

In [4]:

a_s = np.squeeze(a)
print(a_s)

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

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

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

In [5]:

print(a_s.shape)

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

(2, 3)

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

In [6]:

print(np.shares_memory(a, a_s))

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

True

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

In [7]:

a_s_copy = np.squeeze(a).copy()

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

In [8]:

print(np.shares_memory(a, a_s_copy))

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

False

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

In [9]:

print(np.squeeze(a, 0))

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

[[[[0]
[1]
[2]]]

[[[3]
[4]
[5]]]]

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

In [10]:

print(np.squeeze(a, 0).shape)

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

(2, 1, 3, 1)

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

In [11]:

# print(np.squeeze(a, 1))
# ValueError: cannot select an axis to squeeze out which has size not equal to one

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

In [12]:

# print(np.squeeze(a, 5))
# AxisError: axis 5 is out of bounds for array of dimension 5

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

In [13]:

print(np.squeeze(a, -1))

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

[[[[0 1 2]]

[[3 4 5]]]]

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

In [14]:

print(np.squeeze(a, -1).shape)

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

(1, 2, 1, 3)

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

In [15]:

print(np.squeeze(a, -3))

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

[[[[0]
[1]
[2]]

[[3]
[4]
[5]]]]

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

In [16]:

print(np.squeeze(a, -3).shape)

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

(1, 2, 3, 1)

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

In [17]:

print(np.squeeze(a, (0, -1)))

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

[[[0 1 2]]

[[3 4 5]]]

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

In [18]:

print(np.squeeze(a, (0, -1)).shape)

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

(2, 1, 3)

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

In [19]:

# print(np.squeeze(a, (0, 1)))
# ValueError: cannot select an axis to squeeze out which has size not equal to one

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

In [20]:

print(a.squeeze())

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

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

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

In [21]:

print(a.squeeze().shape)

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

(2, 3)

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

In [22]:

print(a.squeeze((0, -1)))

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

[[[0 1 2]]

[[3 4 5]]]

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

In [23]:

print(a.squeeze((0, -1)).shape)

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

(2, 1, 3)

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

In [24]:

a_s = a.squeeze()
print(a_s)

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

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

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

In [25]:

print(np.shares_memory(a, a_s))

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

True

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

In [26]:

print(a)

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

[[[[[0]
[1]
[2]]]

[[[3]
[4]
[5]]]]]

``````