why do we need np.squeeze()?

后端 未结 4 661
北恋
北恋 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: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.

提交回复
热议问题