[英]What is the argument being passed to `np.ones` and how does it work here?
I actually wanted to multiply the array into 3 and tried to use * 3 inside the shape.我实际上想将数组乘以 3 并尝试在形状内使用 * 3 。 I then realized it has to be outside the np.ones like
np.ones((1,2))*4
.然后我意识到它必须在 np.ones 之外,如
np.ones((1,2))*4
。 But was wondering why this is producing exponential results.但想知道为什么这会产生指数结果。 Can someone please explain to me the below behavior?
有人可以向我解释以下行为吗?
np.ones((1,2)*1)
returns array([[1., 1.]])
np.ones((1,2)*1)
返回array([[1., 1.]])
np.ones((1,2)*2)
returns np.ones((1,2)*2)
返回
array([[[[1., 1.]],
[[1., 1.]]]])
np.ones((1,2)*3)
returns np.ones((1,2)*3)
返回
array([[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]])
Similarly, np.ones((1,2)*4)
returns同样,
np.ones((1,2)*4)
返回
array([[[[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]],
[[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]]]])
Unfortunately, the documentation doesn't have any explanation on this.不幸的是, 文档对此没有任何解释。
np.ones
accepts a shape
parameter and returns an ND array according to your specification. np.ones
接受一个shape
参数并根据您的规范返回一个 ND 数组。 For example, with np.ones((10,))
, you get a 1D array with 10 elements... np.ones((3, 5))
will give you a 2D array of size 3x5 with 3*5=15 elements,... and so on.例如,使用
np.ones((10,))
,您将获得一个包含 10 个元素的一维数组... np.ones((3, 5))
将为您提供一个大小为 3x5 且 3*5=15 的二维数组元素……等等。
Now, you've done (for example) (1, 2) * 3
, which if you run in a python REPL will show现在,您已经完成(例如)
(1, 2) * 3
,如果您在 python REPL 中运行将显示
(1, 2) * 3
# (1, 2, 1, 2, 1, 2)
Passing this to np.ones
will return a 6D array of shape (1, 2, 1, 2, 1, 2)
with 8 elements.将此传递给
np.ones
将返回具有 8 个元素的形状为(1, 2, 1, 2, 1, 2)
的 6D 数组。
np.ones((1, 2)*3)
array([[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]])
_.shape
# (1, 2, 1, 2, 1, 2)
And similar, for the others.和其他人类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.