Skip to content
Advertisement

Delete values over the diagonal in a matrix with python

I have the next problem with a matrix in python and numpy

given this matrix

     Cmpd1  Cmpd2   Cmpd3   Cmpd4
Cmpd1   1   0.32    0.77    0.45
Cmpd2   0.32    1   0.14    0.73
Cmpd3   0.77    0.14    1   0.29
Cmpd4   0.45    0.73    0.29    1

i want to obtain this:

    Cmpd1   Cmpd2   Cmpd3   Cmpd4
Cmpd1   1           
Cmpd2   0.32    1       
Cmpd3   0.77    0.14    1   
Cmpd4   0.45    0.73    0.29    1

I was trying with np.diag() but doesnt works

Thanks!

Advertisement

Answer

Use np.tril(a) to extract the lower triangular matrix. Refer this : https://docs.scipy.org/doc/numpy/reference/generated/numpy.tril.html

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement