-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------
import numpy as np
A=np.random.randn(4, 3)
B=np.sum(A, axis=1, keepdims=True) # axis=1时,按照行计算; axis=0时,按照列计算
print("A="+str(A))
print("B="+str(B)) result:
A=[[-0.02149271 -1.0911196 -0.63240592]
[-0.11458854 -0.18210595 0.82210656]
[ 0.39105364 -0.97201463 -0.71820102]
[ 0.30185741 -0.50767254 -0.73277816]]
B=[[-1.74501822]
[ 0.52541207]
[-1.29916201]
[-0.93859329]]
-----------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
答案仅供参考