scipy.sparse.csr_array.

count_nonzero#

csr_array.count_nonzero(axis=None)[source]#

Number of non-zero entries, equivalent to

np.count_nonzero(a.toarray(), axis=axis)

Unlike the nnz property, which return the number of stored entries (the length of the data attribute), this method counts the actual number of non-zero entries in data.

Duplicate entries are summed before counting.

Parameters:
axis{-2, -1, 0, 1, None} optional

Count nonzeros for the whole array, or along a specified axis.

Added in version 1.15.0.

Returns:
numpy array

A reduced array (no axis axis) holding the number of nonzero values for each of the indices of the nonaxis dimensions.

Notes

If you want to count nonzero and explicit zero stored values (e.g. nnz) along an axis, two fast idioms are provided by numpy functions for the common CSR, CSC, COO formats.

For the major axis in CSR (rows) and CSC (cols) use np.diff:

>>> import numpy as np
>>> import scipy as sp
>>> A = sp.sparse.csr_array([[4, 5, 0], [7, 0, 0]])
>>> major_axis_stored_values = np.diff(A.indptr)  # -> np.array([2, 1])

For the minor axis in CSR (cols) and CSC (rows) use numpy.bincount with minlength A.shape[1] for CSR and A.shape[0] for CSC:

>>> csr_minor_stored_values = np.bincount(A.indices, minlength=A.shape[1])

For COO, use the minor axis approach for either axis:

>>> A = A.tocoo()
>>> coo_axis0_stored_values = np.bincount(A.coords[0], minlength=A.shape[1])
>>> coo_axis1_stored_values = np.bincount(A.coords[1], minlength=A.shape[0])

Examples

>>> A = sp.sparse.csr_array([[4, 5, 0], [7, 0, 0]])
>>> A.count_nonzero(axis=0)
array([2, 1, 0])