Python 为什么我们需要 np.squeeze()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34990652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:50:51  来源:igfitidea点击:

why do we need np.squeeze()?

pythonnumpy

提问by

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

很多时候,数组被压缩np.squeeze()。在文档中,它说

Remove single-dimensional entries from the shape of a.

从 a 的形状中删除一维条目。

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

但是我一直在想:为什么在一个形状为零,无量纲项?或者换种方式:为什么都a.shape = (2,1)(2,)存在吗?

采纳答案by Christoph

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_arrayis actually two-dimensional and did not have an implicit squeezeat some point. This would probably obfuscate code far more than the occasional squeeze, which does a clearly specified thing.

除了两者之间的数学差异之外,还有可预测性问题。如果遵循了您的建议,则您绝不能依赖数组的维度。因此,任何形式的表达式my_array[x,y]都需要替换为首先检查是否my_array实际上是二维的并且squeeze在某些时候没有隐含的东西。这可能会比偶尔的 更混淆代码squeeze,后者做了明确指定的事情。

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 squeezeto implicit array conversion.

在精神的Python的禅宗,还Explicit is better than implicit,我们也可以说,我们应该更喜欢明确的squeeze隐式阵列转换。

回答by benj

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)

其次,您可能还想将早期尺寸 egashape = (1,2,2) 压缩到 a.shape = (2,2)

回答by N.S

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 linkfrom tutorials point for example.

这可以帮助您摆脱无用的一维数组,例如 using [7,8,9]代替[[[7,8,9]]][[1,2,3],[4,5,6]]代替[[[[1,2,3],[4,5,6]]]]。例如,从教程点检查此链接

回答by taless

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

当你压缩一个 (2,1) 数组时,你会得到 (2,) 既可以用作 (2,1) 又可以用作 (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:

这不会发生在 (2,1) 数组中:

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