I have a 3d numpy
array that looks like so:
>>> g
array([[[ 1., 1., 1., 1., 1.],
[ 0., 0., 0., 0., 0.],
[ 1., 2., 3., 4., 6.]],
[[ 0., 0., 0., 0., 0.],
[11., 22., 33., 44., 66.],
[ 0., 0., 0., 0., 0.]]])
gs = g.sum(axis=1)
that will result in this array:>>> gs
array([[ 2., 3., 4., 5., 7.],
[11., 22., 33., 44., 66.]])
How do I stack this summing array to the original one as the forth vector in each of the two inside groups? The expected result would be:
>>> g
array([[[ 1., 1., 1., 1., 1.],
[ 0., 0., 0., 0., 0.],
[ 1., 2., 3., 4., 6.],
[ 2., 3., 4., 5., 7.]],
[[ 0., 0., 0., 0., 0.],
[11., 22., 33., 44., 66.],
[ 0., 0., 0., 0., 0.],
[ 11., 22., 33., 44., 66.]]])
gss = g.sum(axis=0)
and looks like so:>>> gss
array([[ 1., 1., 1., 1., 1.],
[11., 22., 33., 44., 66.],
[ 1., 2., 3., 4., 6.]])
How do I stack it to the original array to get the result shown below?
>>> g
array([[[ 1., 1., 1., 1., 1.],
[ 0., 0., 0., 0., 0.],
[ 1., 2., 3., 4., 6.]],
[[ 0., 0., 0., 0., 0.],
[11., 22., 33., 44., 66.],
[ 0., 0., 0., 0., 0.]],
[[ 1., 1., 1., 1., 1.],
[11., 22., 33., 44., 66.],
[ 1., 2., 3., 4., 6.]]])
np.concatenate([g, g.sum(axis=1, keepdims=True)], axis=1)
Equivalent for axis=0