why do we need np.squeeze()?

后端 未结 4 618
北恋
北恋 2021-01-30 12:56

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

Remove single-dimensional entries from the shape of a.

相关标签:
4条回答
  • 2021-01-30 13:36

    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)

    0 讨论(0)
  • 2021-01-30 13:38

    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)
    
    0 讨论(0)
  • 2021-01-30 13:39

    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.

    0 讨论(0)
  • 2021-01-30 13:45

    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.

    0 讨论(0)
提交回复
热议问题