How does np.squeeze work?

np.squeeze removes axes of length one.

train_set_y[:,index].shape is (1,), since we keep the first axis.
To concatenate strings we need the string ‘cat’ not [‘cat’]; hence, we squeeze the array to retrieve the scalar value it contains.
Alternatively, you could access the scalar directly by using train_set_y[0,index]. In that case, np.squeeze is redundant.

Example:

two = np.array([[[[[[2]]]]]])
print(two.shape, np.squeeze(two).shape)

yields

(1, 1, 1, 1, 1, 1) ()

i.e, a scalar in the last case.

4 Likes