我有一個形狀為 (100, 4) 的二維 numpy 陣列 X。我想找到該陣列每一行的總和,并將其存盤在一個形狀為 (100,0) 的新 numpy 陣列 x_new 中。到目前為止我所做的不起作用。有什么建議 ?。下面是我的做法。
x_new = np.empty([100,0])
for i in range(len(X)):
array = np.append(x_new, sum(X[i]))
uj5u.com熱心網友回復:
sum在二維陣列上使用該方法:
In [8]: x = np.arange(12).reshape(3,4)
In [9]: x
Out[9]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [10]: x.sum(axis=1)
Out[10]: array([ 6, 22, 38])
In [12]: x.sum(axis=1, keepdims=True)
Out[12]:
array([[ 6],
[22],
[38]])
In [13]: _.shape
Out[13]: (3, 1)
參考:https : //numpy.org/doc/stable/reference/generated/numpy.sum.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338389.html
