我实际上想将数组乘以3,并尝试在形状内部使用* 3。然后,我意识到它必须位于np.ones((1,2))*4
之类的np.one之外。但是想知道为什么这会产生指数结果。有人可以向我解释以下行为吗?
np.ones((1,2)*1)
返回array([[1., 1.]])
np.ones((1,2)*2)
返回
array([[[[1., 1.]],
[[1., 1.]]]])
np.ones((1,2)*3)
返回
array([[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]])
类似地,np.ones((1,2)*4)
返回
array([[[[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]],
[[[[[[1., 1.]],
[[1., 1.]]]],
[[[[1., 1.]],
[[1., 1.]]]]]]]])
不幸的是,documentation对此没有任何解释。
答案 0 :(得分:1)
np.ones
接受一个shape
参数,并根据您的规范返回一个N-D数组。例如,使用np.ones((10,))
,您将获得一个包含10个元素的1D数组... np.ones((3, 5))
将为您提供一个3x5大小的3D5 = 15个元素的2D数组,等等。
现在,您已经完成了(1, 2) * 3
,例如,如果您在python中运行,REPL将显示
(1, 2) * 3
# (1, 2, 1, 2, 1, 2)
将其传递给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)
其他类似的东西。