why do we need np.squeeze()?

Question:

Very often, arrays are squeezed with np.squeeze(). In the documentation, it says

Remove single-dimensional entries from the shape of a.

However I’m still wondering: Why are zero and nondimensional entries in the shape of a? Or to put it differently: Why do both a.shape = (2,1) and (2,) exist?

Asked By: user2553692

||

Answers:

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)

Answered By: benj

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.

Answered By: Christoph

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.

Answered By: N.S

When you squeeze a (2,1) array, you get (2,) which works as both (2,1) and (1,2):

>>> a = np.ones(2)
>>> a.shape
(2,)
>>> a.T.shape
(2,)
>>> X = np.ones((2,2))*2
>>> np.dot(a,X)
[4. 4.]
>>> np.dot(X,a)
[4. 4.]

This cannot happen with a (2,1) array:

>>> b = np.ones((2,1))
>>> np.dot(b,X)
Traceback (most recent call last):
ValueError: shapes (2,1) and (2,2) not aligned: 1 (dim 1) != 2 (dim 0)
Answered By: taless
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.