The official documentation (as of NumPy 1.13. 0 suggests that the inverse of np. squeeze() is np.
You can use numpy. squeeze() to remove all dimensions of size 1 from the NumPy array ndarray . squeeze() is also provided as a method of ndarray .
To reduce a multi-dimensional array, use the np. ufunc. reduce() method in Python Numpy.
Singular dimensions (value 1) can be removed with indexing, [0] , or squeeze . reshape also removes demensions (or adds them), but you have to pay attention to the total number of elements. If the old shape had 12 elements, the new has to have 12 as well.
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.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With