问题描述
好吧,所以我发现可以有形状为0的数组.
对于您只有0作为唯一尺寸的情况,这对我来说很有意义.这是一个空数组.
Okay, so I found out that you can have arrays with 0s in their shape.
For the case where you have 0 as the only dimension, this makes sense to me. It is an empty array.
np.zeros(0)
但是如果您有这样的情况:
But the case where you have something like this:
np.zeros((0, 100))
让我感到困惑.为什么这样定义?
Is confusing for me. Why is it defined like this?
推荐答案
据我所知,这只是表示空数组的一种多余方法.如果您有空"行,对于python似乎并不重要.
As far as I know it's just a redundant way to express an empty array. It doesn't seems to matter for python if you have rows of "emptiness".
假设我们有一个给定数组a:
Let's say we have a give array a:
import numpy as np
a = np.zeros((0,100))
如果我们打印出全部,我们得到的就是空数组本身:
If we print a all we get is the empty array itself:
print(a)
>>> []
此外,我们实际上可以看到,尽管保持了它的形状,
Moreover we can actually see that despite this a maintain it's shape"
np.shape(a)
>>> (0, 100)
但是,如果您尝试按位置访问给定的元素,例如:
But if you try to access a given element by position, e.g:
print(a[0])
或
print(a[0][0])
您收到IndexError:
You get an IndexError :
IndexError: index 0 is out of bounds for axis 0 with size 0
因此,我相信,尽管您为空数组指定了形状,但它们的数学含义是相同的.
Therefore I believe that the mathematical meaning of the empty arrays, despite the shape you assign to them, is the same.
这篇关于以零开头的numpy形状是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!