I want to find all rows in a Compressed Sparse Row (CSR) matrix that contain only zero values. Is there any scipy function that I can use to find?

+1 vote

Best answer

You can use the* count_nonzero() *method to count the number of non-zero entries in each row of a CSR matrix. Rows that return 0 will have all columns 0.

Here is an example:

*import numpy as np*

*from scipy.sparse import csr_matrix# datarow = np.array([0, 0, 2, 2, 2, 3])col = np.array([0, 2, 0, 1, 2, 2])data = np.array([1, 2, 3, 4, 5, 6])# Create CSR matrixcsr_data = csr_matrix((data, (row, col)), shape=(4, 4))zero_rows = []for i in range(csr_data.shape[0]): if csr_data[i].count_nonzero() == 0: zero_rows.append(i)print("rows with only 0: ", zero_rows)*

The above code will print [1] as row 1 has all columns 0.