-
Notifications
You must be signed in to change notification settings - Fork 52
Closed
Labels
API extensionAdds new functions or objects to the API.Adds new functions or objects to the API.AcceptedRFC feature request which has been accepted.RFC feature request which has been accepted.RFCRequest for comments. Feature requests and proposed changes.Request for comments. Feature requests and proposed changes.topic: StatisticsStatistics.Statistics.
Milestone
Description
This RFC proposes a new addition to the array API specification for counting the number of "non-zero" (i.e., truthy) values in an array.
Overview
Based on array comparison data, the API is available across all major array libraries in the PyData ecosystem.
count_nonzero was originally identified in #187 as a potential standardization candidate and has usage within downstream libraries (e.g., sklearn, SciPy).
Prior art
- NumPy: https://numpy.org/doc/stable/reference/generated/numpy.count_nonzero.html
- CuPy: https://docs.cupy.dev/en/stable/reference/generated/cupy.count_nonzero.html
- does not support
keepdims.
- does not support
- Dask: https://docs.dask.org/en/stable/generated/dask.array.count_nonzero.html
- does not support
keepdims.
- does not support
- JAX: https://jax.readthedocs.io/en/latest/_autosummary/jax.numpy.count_nonzero.html
- PyTorch: https://pytorch.org/docs/stable/generated/torch.count_nonzero.html
- does not support
keepdimsand was not discussed upon initial addition (ref).
- does not support
- TensorFlow: https://www.tensorflow.org/api_docs/python/tf/math/count_nonzero
Proposal
def count_nonzero(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array- When
axisisNone, the function should count the number of non-zero elements along a flattened array. - The function should return an array having the default index data type.
Questions
- In contrast to
sumand other reductions, support forkeepdimsis less common among array libraries. Why this is the case is not clear. Are there any reasons whykeepdimsshould not be standardized?
Metadata
Metadata
Assignees
Labels
API extensionAdds new functions or objects to the API.Adds new functions or objects to the API.AcceptedRFC feature request which has been accepted.RFC feature request which has been accepted.RFCRequest for comments. Feature requests and proposed changes.Request for comments. Feature requests and proposed changes.topic: StatisticsStatistics.Statistics.
Type
Projects
Status
Stage 1