说,我有一个像这样的numpy数组:
[[ 1 2 3 5]
[ 4 5 6 8]
[ 7 8 9 11]]
我想得到每一行的总和(col 0 + col 2)和(col1 + col3)。 我知道它可能是基本的,但不能得到它。请帮助
答案 0 :(得分:4)
嗯,是的,一旦你知道了numpy数组的赋值方案,它就非常基本了:
让
x = [[ 1 2 3 5]
[ 4 5 6 8]
[ 7 8 9 11]]
y1 = x[:,0]+x[:,2] # Sum of columns 0 and 2
y1 = array([4,10,16])
y2 = x[:,1]+x[:,3] # Sum of columns 1 and 3
y2 = array([7,13,19])
答案 1 :(得分:1)
这应该可以解决问题:
import numpy as np
mat=np.arange(12).reshape(3,4)
A=np.sum(mat,axis=0)[0::2]
B=np.sum(mat,axis=0)[1::2]
A =(col 0 + col 2)
B =(col1 + col3)
答案 2 :(得分:0)
<强>设置强>
for
<强>解决方案强>
Out[99]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])