Skip to content

RFC: add count_nonzero for counting the number of "non-zero" values #794

Closed
@kgryte

Description

@kgryte

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

Proposal

def count_nonzero(x: array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False) -> array
  • When axis is None, 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 sum and other reductions, support for keepdims is less common among array libraries. Why this is the case is not clear. Are there any reasons why keepdims should not be standardized?

Metadata

Metadata

Assignees

No one assigned

    Labels

    API extensionAdds new functions or objects to the API.AcceptedRFC feature request which has been accepted.RFCRequest for comments. Feature requests and proposed changes.topic: StatisticsStatistics.

    Type

    No type

    Projects

    Status

    Stage 1

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions