2017-06-12 52 views
1

我是Python和numpy的新手,所以我只是運行示例代碼並嘗試調整它們以便理解。我遇到了一些關於numpy.sum的代碼,其中axis參數,但我無法運行。在某段時間(閱讀scipy文檔,嘗試實驗)後,我通過使用axis = (1,2,3)而不是axis = 1來運行它。具有軸行爲的python numpy sum函數

事情是,無論我搜索,他們只寫axis = 1讓它工作。

我正在使用Python 3.5.3,numpy 1.12.1 是否有一個numpy/python版本在行爲上有很大的差異?或者我只是以某種方式配置它是錯誤的?

import numpy as np 
from past.builtins import xrange 


# sample data 
X = np.arange(1, 4*4*3*5+1).reshape(5, 4, 4, 3) 
Y = np.arange(5, 4*4*3*8+5).reshape(8, 4, 4, 3) 
Xlen = X.shape[0] 
Ylen = Y.shape[0] 

# allocate some space for whatever calculation 
rs = np.zeros((Xlen, Ylen)) 
rs1 = np.zeros((Xlen, Ylen)) 

# calculate the result with 2 loops 
for i in xrange(Xlen): 
    for j in xrange(Ylen): 
     rs[i, j] = np.sum(X[i] + Y[j]) 

# calculate the result with one loop only 
for i in xrange(Xlen): 
    rs1[i, :] = np.sum(Y + X[i], axis=(1,2,3)) 

print(rs1 == rs) # same result 

# also with one loop, as everywhere on the internet: 
for i in xrange(Xlen): 
    rs1[i, :] = np.sum(Y + X[i], axis=1) 
    # ValueError: could not broadcast input array from shape (8,4,3) into shape (8) 
+0

'軸= 1'款項你指定一個維度。結果是一個尺寸爲'ndim-1'的數組;在你的情況下,它有三個維度和形狀'(8,4,3)'。這與輸出數組rs1 [i,:]'不兼容,它只有兩個維度。 – MaxPowers

回答

0
axis : None or int or tuple of ints, optional 
    ... 
    If axis is a tuple of ints, a sum is performed on all of the axes 
    specified in the tuple instead of a single axis or all the axes as 
    before. 

使用元組的能力是加成(V1.7,2013年)。我沒有用太多,當我在MATLAB中需要它時,我使用了重複的和,例如

In [149]: arr = np.arange(24).reshape(2,3,4) 
In [150]: arr.sum(axis=(1,2)) 
Out[150]: array([ 66, 210]) 
In [151]: arr.sum(axis=2).sum(axis=1) 
Out[151]: array([ 66, 210]) 

在做你需要記住的順序是求和的軸變化的數量(除非你使用keepdims,本身就是一個新望參數)。


X,Y總和:

In [160]: rs = np.zeros((Xlen, Ylen),int) 
    ...: rs1 = np.zeros((Xlen, Ylen),int) 
    ...: 
    ...: # calculate the result with 2 loops 
    ...: for i in range(Xlen): 
    ...: for j in range(Ylen): 
    ...:  rs[i,j] = np.sum(X[i] + Y[j]) 
    ...: 
In [161]: rs 
Out[161]: 
array([[ 2544, 4848, 7152, 9456, 11760, 14064, 16368, 18672], 
     [ 4848, 7152, 9456, 11760, 14064, 16368, 18672, 20976], 
     [ 7152, 9456, 11760, 14064, 16368, 18672, 20976, 23280], 
     [ 9456, 11760, 14064, 16368, 18672, 20976, 23280, 25584], 
     [11760, 14064, 16368, 18672, 20976, 23280, 25584, 27888]]) 

可以在沒有環被複制。

In [162]: X.sum((1,2,3)) 
Out[162]: array([ 1176, 3480, 5784, 8088, 10392]) 
In [163]: Y.sum((1,2,3)) 
Out[163]: array([ 1368, 3672, 5976, 8280, 10584, 12888, 15192, 17496]) 
In [164]: X.sum((1,2,3))[:,None] + Y.sum((1,2,3)) 
Out[164]: 
array([[ 2544, 4848, 7152, 9456, 11760, 14064, 16368, 18672], 
     [ 4848, 7152, 9456, 11760, 14064, 16368, 18672, 20976], 
     [ 7152, 9456, 11760, 14064, 16368, 18672, 20976, 23280], 
     [ 9456, 11760, 14064, 16368, 18672, 20976, 23280, 25584], 
     [11760, 14064, 16368, 18672, 20976, 23280, 25584, 27888]]) 

np.sum(X[i] + Y[j]) =>np.sum(X[i]) + np.sum(Y[j])sum(X[i])總計X[i](axis = None)的所有元素。除了1st,X.sum(axis=(1,2,3))[i]以外,所有軸都是相同的。

In [165]: X[0].sum() 
Out[165]: 1176 
In [166]: X.sum((1,2,3))[0] 
Out[166]: 1176 
In [167]: X.sum(1).sum(1).sum(1)[0] 
Out[167]: 1176 

至於廣播錯誤,看片:

In [168]: rs1[i,:] 
Out[168]: array([0, 0, 0, 0, 0, 0, 0, 0]) # shape (8,) 
In [169]: (Y+X[i]).shape # (8,4,4,3) + (4,4,3) 
Out[169]: (8, 4, 4, 3) 
In [170]: (Y+X[i]).sum(1).shape # sums axis 1, ie one of the 4's 
Out[170]: (8, 4, 3) 
+0

我第一次結束了使用重複的總和,但後來我發現使用元組更容易閱讀我的情況。 不過,我不明白爲什麼很多人只有在使用axis = 1的情況下才能使用,這會導致不同的形狀。 – Nin

+0

我期望'tuple'選項的主要理由是可讀性。 – hpaulj

0

要只axis=1得到相同的結果寫的,我們能做到事前重塑數據集的一招。

X = np.reshape(X, (X.shape[0], -1)) 
Y = np.reshape(Y, (Y.shape[0], -1)) 

for i in xrange(Xlen): 
    rs[i, :] = np.sum(Y + X[i], axis=1) 
print(rs) 

其結果是:僅

[[ 2544. 4848. 7152. 9456. 11760. 14064. 16368. 18672.] 
[ 4848. 7152. 9456. 11760. 14064. 16368. 18672. 20976.] 
[ 7152. 9456. 11760. 14064. 16368. 18672. 20976. 23280.] 
[ 9456. 11760. 14064. 16368. 18672. 20976. 23280. 25584.] 
[ 11760. 14064. 16368. 18672. 20976. 23280. 25584. 27888.]]