From what I understand, x.shape required otherwise output would be an array, counting each row for number of non zeroes, i.e. in above, row 1 would be 4 (4 nonzeroes, 1 zero), row 2 = 4, row 3 = 5, row 4 = 5, row 5 = 4. (please correct me if i am wrong here)
is x.shape returning it as boolean for the columns? why columns and not rows?
x.shape is just indexing of getting the number of columns from array x.
From the code, you are trying to check if the number of non-zeros per row is the same as the number of columns of x. Therefore mask will be a boolean array, but x.shape will have the number of columns in x, np.count_nonzero(x,axis=1) is the number of columns that have no zero per row in x. https://numpy.org/doc/stable/reference/generated/numpy.count_nonzero.html