[英]What do we mean by this Numpy shape?
我遇到了这个Python语句,但无法理解它的含义,尤其是括号之间的部分:
np.zeros(1+x.shape[1])
我试图通过一个简单的例子模仿它的行为,但得到一个tuple index out of range
错误。
你能澄清上述数组的参数含义吗? 非常感谢一个例子。
谢谢。
这是一个玩具代码,可以帮助您更好地理解
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> x.shape
(2, 3)
>>> x.shape[1]
3
>>> np.zeros(1+x.shape[1])
array([ 0., 0., 0., 0.])
在这种情况下(2, 3)
x.shape
将数组的形状作为元组返回(no of rows, no of columns)
(2, 3)
。 因此, x.shape[1]
是数组中列的数量。 使用给定维度创建一个填充零( np.zeros(...)
)的新数组: 1+3
这意味着:创建一个带有零的1D numpy数组,其长度等于numpy数组x
的列数。
>>> a = np.array([[1,2,1],[3,4,5]])
>>> print a.shape
(2L, 3L)
>>> b = np.zeros(1+a.shape[1])
>>> print b
[ 0. 0. 0. 0.]
b
大小等于1+(number of cols in a)
= 1+3
= 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.