def count_nonzero(X, axis=None, sample_weight=None):
 if (axis == (-1)):
  axis = 1
 elif (axis == (-2)):
  axis = 0
 elif (X.format != 'csr'):
  raise TypeError('Expected CSR sparse format, got {0}'.format(X.format))
 if (axis is None):
  if (sample_weight is None):
   return X.nnz
  else:
   return np.dot(np.diff(X.indptr), sample_weight)
 elif (axis == 1):
  out = np.diff(X.indptr)
  if (sample_weight is None):
   return out
  return (out * sample_weight)
 elif (axis == 0):
  if (sample_weight is None):
   return bincount(X.indices, minlength=X.shape[1])
  else:
   weights = np.repeat(sample_weight, np.diff(X.indptr))
   return bincount(X.indices, minlength=X.shape[1], weights=weights)
 else:
  raise ValueError('Unsupported axis: {0}'.format(axis))

def count_nonzero ( X , axis = None , sample_weight = None ) : if ( axis == ( - 1 ) ) : axis = 1 elif ( axis == ( - 2 ) ) : axis = 0 elif ( X . format != 'csr' ) : raise TypeError ( 'Expected CSR sparse format, got {0}' . format ( X . format ) ) if ( axis is None ) : if ( sample_weight is None ) : return X . nnz else : return np . dot ( np . diff ( X . indptr ) , sample_weight ) elif ( axis == 1 ) : out = np . diff ( X . indptr ) if ( sample_weight is None ) : return out return ( out * sample_weight ) elif ( axis == 0 ) : if ( sample_weight is None ) : return bincount ( X . indices , minlength = X . shape [ 1 ] ) else : weights = np . repeat ( sample_weight , np . diff ( X . indptr ) ) return bincount ( X . indices , minlength = X . shape [ 1 ] , weights = weights ) else : raise ValueError ( 'Unsupported axis: {0}' . format ( axis ) )
counts the number of non-zero values in the array .