8000 np.count_nonzero() should accept axis parameter · Issue #391 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

np.count_nonzero() should accept axis parameter #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kernc opened this issue Aug 27, 2012 · 9 comments
Closed

np.count_nonzero() should accept axis parameter #391

kernc opened this issue Aug 27, 2012 · 9 comments

Comments

@kernc
Copy link
kernc commented Aug 27, 2012

Currently, np.count_nonzero() accepts only the input array. I propose adding axis parameter similar to that of functions np.sum() and np.max() etc.

>>> A = np.arange(6).reshape(2,3)
>>> np.count_nonzero(A)  # with axis=None, count_nonzero in flattened A
5
>>> np.count_nonzero(A, axis=0)
array([1, 2, 2])
>>> np.count_nonzero(A, axis=1)
array([2, 3])

I think the alternative (A != 0).sum(axis=rows_or_cols) isn't as clear.

@charris
Copy link
Member
charris commented Feb 17, 2014

Sounds reasonable.

@jaimefrio
Copy link
Member

I wrote this elsewhere, but if no one else wants to try it, I can go on an "add axis" spree: I have working code for bincount (will try to make it into a PR tonight), and have searchsorted, interp, and now count_nonzero in the pipeline. Is there any other obvious one I am forgetting about?

@herr-biber
Copy link
Contributor

Any news on this? I'd be interested in this feature as well.

@juliantaylor
Copy link
Contributor

an alternative might be to try and adapt sum(bool) to use the much faster non-casting count_nonzero code.
sum is already a full ufunc with all bells and whistles so one gets (extended) axis for free

@seberg
Copy link
Member
seberg commented Apr 23, 2014

I am unsure if the ufunc reduce supports mixed type inner loops.

@WarrenWeckesser
Copy link
Member

+1 for adding an axis argument to count_nonzero().

@JaimieMurdock
Copy link

+1 here as well.

@tentrillion
Copy link

Also +1 here.

@lzkelley
Copy link
Contributor

+1

@shoyer shoyer closed this as completed in 0fc9e45 Aug 5, 2016
luyahan pushed a commit to plctlab/numpy that referenced this issue Apr 25, 2024
feat: Add vmaxv[q]_[s8|s16|s32|u8|u16|u32|f32|f64]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants
0