why do we need np.squeeze()?

PythonNumpy

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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)

Solution 4 - Python

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2553692View Question on Stackoverflow
Solution 1 - PythonChristophView Answer on Stackoverflow
Solution 2 - PythonN.SView Answer on Stackoverflow
Solution 3 - PythonbenjView Answer on Stackoverflow
Solution 4 - PythontalessView Answer on Stackoverflow