count_nonzero axis argument?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

count_nonzero axis argument?

Matthew Brett
Hi,

Is there any reason that np.count_nonzero should not take an axis
argument?  As in:

>>> np.better_count_nonzero([[10, 11], [0, 3]], axis=1)
array([2, 1])

It would be much more useful if it did...

Cheers,

Matthew
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: count_nonzero axis argument?

Sebastian Berg
On Mon, 2018-09-17 at 12:37 +0100, Matthew Brett wrote:
> Hi,
>
> Is there any reason that np.count_nonzero should not take an axis
> argument?  As in:
>

No, sounds like an obvious improvement, but as also with those, someone
has to volunteer to do it...
Coding it will probably mean adding the NpyIter and possibly fast paths
(not sure about the state of count nonzero), but should not be very
difficult.

- Sebastian



> > > > np.better_count_nonzero([[10, 11], [0, 3]], axis=1)
>
> array([2, 1])
>
> It would be much more useful if it did...
>
> Cheers,
>
> Matthew
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
>

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: count_nonzero axis argument?

Juan Nunez-Iglesias
In reply to this post by Matthew Brett
On Mon, Sep 17, 2018, at 9:37 PM, Matthew Brett wrote:
> >>> np.better_count_nonzero([[10, 11], [0, 3]], axis=1)
> array([2, 1])
>
> It would be much more useful if it did...

You might know about this already, but I not too long ago discovered np.apply_along_axis [1], which is a magical function that makes all functions axis aware. Obviously I would support adding an axis keyword to count_nonzero, but in the meantime this function gets you there quickly!

Juan.

.. [1] https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.apply_along_axis.html
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: count_nonzero axis argument?

Warren Weckesser-2
In reply to this post by Matthew Brett


On Mon, Sep 17, 2018 at 7:38 AM Matthew Brett <[hidden email]> wrote:
Hi,

Is there any reason that np.count_nonzero should not take an axis
argument?  As in:

>>> np.better_count_nonzero([[10, 11], [0, 3]], axis=1)
array([2, 1])




Warren


It would be much more useful if it did...

Cheers,

Matthew
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: count_nonzero axis argument?

Matthew Brett
On Mon, Sep 17, 2018 at 2:24 PM, Warren Weckesser
<[hidden email]> wrote:

>
>
> On Mon, Sep 17, 2018 at 7:38 AM Matthew Brett <[hidden email]>
> wrote:
>>
>> Hi,
>>
>> Is there any reason that np.count_nonzero should not take an axis
>> argument?  As in:
>>
>> >>> np.better_count_nonzero([[10, 11], [0, 3]], axis=1)
>> array([2, 1])
>>
>
>
> It already does (since version 1.12.0):
> https://docs.scipy.org/doc/numpy/reference/generated/numpy.count_nonzero.html

Oops - sorry - I am so used to upgrading all the time, it didn't occur
to me I had an old version in my standard setup.  Thanks, and sorry
for the distraction.

Cheers,

Matthew
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion