why do we need np.squeeze()?

Solution 1:

Besides the mathematical differences between the two things, there is the issue of predictability. If your suggestion was followed, you could at no point rely on the dimension of your array. So any expression of the form my_array[x,y] would need to be replaced by something that first checks if my_array is actually two-dimensional and did not have an implicit squeeze at some point. This would probably obfuscate code far more than the occasional squeeze, which does a clearly specified thing.

Actually, it might even be very hard to tell, which axis has been removed, leading to a whole host of new problems.

In the spirit of The Zen of Python, also Explicit is better than implicit, we can also say that we should prefer explicit squeeze to implicit array conversion.

Solution 2:

This helps you get rid of useless one dimension arrays like using [7,8,9] instead of [[[7,8,9]]] or [[1,2,3],[4,5,6]] instead of [[[[1,2,3],[4,5,6]]]]. Check this link from tutorials point for example.

Solution 3:

One example of the importance is when multiplying arrays. Two 2-dimensional arrays will multiply each value at a time

e.g.

>>> x = np.ones((2, 1))*2
>>> y = np.ones((2, 1))*3
>>> x.shape
(2,1)
>>> x*y
array([[ 6.],
       [ 6.]])

If you multiply a 1d array by a 2d array then the behaviour is different

>>> z = np.ones((2,))*3
>>> x*z
array([[ 6.,  6.],
       [ 6.,  6.]])

Secondly, you also might want to squeeze the earlier dimensions e.g. a.shape = (1,2,2) to a.shape = (2,2)