Python 为什么numpy.count\u nonzero(arr)不能与指定的轴值一起工作?

Python 为什么numpy.count\u nonzero(arr)不能与指定的轴值一起工作?,python,arrays,numpy,Python,Arrays,Numpy,我有一个二维数组,希望按行计算值。即使我指定了一个axis值,axis=1,该方法仍然返回一个整数 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_n

我有一个二维数组,希望按行计算值。即使我指定了一个axis值,axis=1,该方法仍然返回一个整数

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
我尝试直接从docs页面复制一个示例,得到了相同的结果

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

array([2, 3])
我正在使用Python 3.6

您知道是什么阻止了该方法返回跨行计数数组吗?

来自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\u nonzero
nonzero
where
)用于确定返回结果所需分配的数组的大小。为此,它不需要axis参数。它只需要快速和简单。如果开发人员将此作为其主要理由,它可以解释为什么
axis
是一个后期添加。

对我来说很有效。你用的是什么numpy版本?我用的是'1.11.3'。我会尝试更新。就在那里让我看看。谢谢你指出我应该抓到的东西。