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]) z"count_nonzero not implemented for Ú