Here I will talk about a more general solution that uses a masked array mask. To illustrate the details, create a lower triangular matrix with only one:
matrix = np.tril(np.ones((5, 5)), 0)
If you did not specify the above terminology, this matrix is ββas follows:
[[ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.], [ 1., 1., 1., 0., 0.], [ 1., 1., 1., 1., 0.], [ 1., 1., 1., 1., 1.]]
Now we want our function to return an average of 1 for each row. Or, in other words, the average along axis 1 is equal to a vector of five. To do this, we created a matrix matrix where records whose values ββare equal to zero are considered invalid . This can be achieved using np.ma.masked_equal :
masked = np.ma.masked_equal(matrix, 0)
Finally, we perform numpy operations in this array, which systematically ignore masked elements (0). With this in mind, we get the desired result:
masked.mean(axis=1)
This should result in a vector whose entries are only one.
In more detail, the output of np.ma.masked_equal(matrix, 0) should look like this:
masked_array(data = [[1.0 -- -- -- --] [1.0 1.0 -- -- --] [1.0 1.0 1.0 -- --] [1.0 1.0 1.0 1.0 --] [1.0 1.0 1.0 1.0 1.0]], mask = [[False True True True True] [False False True True True] [False False False True True] [False False False False True] [False False False False False]], fill_value = 0.0)
This means that the eh on -- values -- considered invalid. This is also shown in the mask attribute of masked arrays as True , which indicates that IT is an invalid element and should therefore be ignored.
Finally, the output of the average operation on this array should be:
masked_array(data = [1.0 1.0 1.0 1.0 1.0], mask = [False False False False False], fill_value = 1e+20)