在额外维度上扩展给定 NumPy 数组的最简单方法是什么?
例如,假设我有
>>> np.arange(4)
array([0, 1, 2, 3])
>>> _.shape
(4,)
>>> expand(np.arange(4), 0, 6)
array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
>>> _.shape
(6, 4)
或者这个,更复杂一点:
>>> np.eye(2)
array([[ 1., 0.],
[ 0., 1.]])
>>> _.shape
(2, 2)
>>> expand(np.eye(2), 0, 3)
array([[[ 1., 0.],
[ 0., 1.]],
[[ 1., 0.],
[ 0., 1.]],
[[ 1., 0.],
[ 0., 1.]]])
>>> _.shape
(3, 2, 2)