2017-06-18 62 views
3

我有一個二維數組,並希望按行計數值。即使我分配一個軸值,axis = 1,方法仍會返回單個整數。爲什麼numpy.count_nonzero(arr)不能使用分配的軸值?

x = rng.randint(10, size=(6,3)) 

x 
Out[120]: 
array([[3, 3, 8], 
     [8, 8, 2], 
     [3, 2, 0], 
     [8, 8, 3], 
     [8, 2, 8], 
     [4, 3, 0]]) 

np.count_nonzero(x) 
Out[121]: 16 

np.count_nonzero(x, axis=1) 
Out[122]: 16 

我試着直接從文檔頁面複製一個例子,並得到了相同的結果。

np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1) 
Out[123]: 5 

其中預計:

array([2, 3]) 

我使用Python 3.6

任何想法可能會保持從跨行返回計數的數組的方法是什麼?

+0

適合我。你在使用什麼_numpy_版本? – DSM

+0

我使用'1.11.3'。我會嘗試更新。 –

回答

1

從V1.12文檔:

axis : int or tuple, optional 
    Axis or tuple of axes along which to count non-zeros. 
    Default is None, meaning that non-zeros will be counted 
    along a flattened version of ``a``. 

    .. versionadded:: 1.12.0 

所以這個軸參數是新望。

count_nonzerononzerowhere)用來確定它需要分配以返回結果的數組的大小。爲此,它不需要軸參數。它只需要快速和簡單。如果開發人員將這種使用看作主要理由,那麼它可以解釋爲什麼axis是最新的。

+0

就在那裏給我看。感謝您指出我應該抓到的東西。 –

相關問題