How to get np.nonzero

code

please can you kindly explain how the outcome line 324 of the array dont seem to understand the 0s,1s,2s

Hi @billy!
In array ‘a’ there are non zero elements:
30 - in 0th row
40 - in 0th row
20 - in 1st row
10 - in 1st row
50 - in 2nd row
60 - in 2nd row
And so is the first array returned in line 324 i.e. array[0, 0, 1, 1, 2, 2] - these are nothing but the row’s index in which the non-zero elements lie.

  1. Now in 0th row, the nonzero elements are at 0th and 1st position. So first two value in second array of output line 324 is 0 and 1
  2. Similarly in 1st row, the nonzero elements are at 1st and 2nd position. So the next two value in the second array of output line 324 is 1, 2
  3. Similarly in 2nd row, the nonzero elements are at 0th and 2nd position. So the next two values in the second array of output line 324 is 0, 2

Combining 1, 2 and 3 the second array in output line is array[0, 1, 1, 2, 0, 2] - these are the indexes of element’s in each row.

2 Likes