<p>There seems to be a scipy method that retains the lower triangle but with the 0s. See: <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.tril.html#scipy.sparse.tril" rel="nofollow noreferrer">https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.tril.html#scipy.sparse.tril</a></p>

CC BY-SA 4.0
2023-11-01T15:43:44.120Z
Thanks! This is pretty helpful too
rimo
CC BY-SA 4.0
2023-10-27T16:36:36.283Z
2023-10-27T16:36:36.283Z
Ram RS
<p>NumPy has a convenient built-in function for accessing the lower-triangular or upper-trianglar entries of an array.
To get the lower-triangular entries, you can use <a href="https://numpy.org/doc/stable/reference/generated/numpy.tril_indices.html" rel="noreferrer"><code>numpy.tril_indices</code></a>
to get the indices of these entries, and then use these indices to extract the entries.</p>
<pre><code>import numpy as np

ar = np.array(
    [[0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]]
)
idx = np.tril_indices(3, -1)
ar[idx]
# outputs array([3, 6, 7])
</code></pre>
<p>This doesn't automatically produce the spacing you want, but formatting the final display is likely context-dependent.</p>

CC BY-SA 4.0
2023-11-01T15:43:35.040Z
This could work for now thank you!!!
rimo
CC BY-SA 4.0
2023-10-27T20:40:49.520Z
2023-10-27T20:40:49.520Z
Harry Richman
<p>I'm using Scipy's pdist function to calculate a distance matrix which is working great but it gives me a symmetric matrix and I want to convert it into a lower-left triangular matrix.</p>
<p>This is what it prints out for me:</p>
<pre><code>  a   b   c   d
a 0 ### ### ###
b ### 0 ### ###
c ### ### 0 ###
d ### ### ### 0
</code></pre>
<p>This is what I want it to be:</p>
<pre><code>###
### ###
### ### ###
</code></pre>
<p>I know there's a function in R to do this (upper.tri) but it just replaces everything to the right of the 0 with more 0s. Is there a better/easier way to get it like this? I'm opening the file in excel and removing everything to the right with the 0 manually right now.</p>

CC BY-SA 4.0
2023-10-27T20:45:23.730Z
From reading the scipy documentation for the [`pdist` function](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html#scipy.spatial.distance.pdist), it seems to suggest it should already only return upper-triangular entries rather than a full matrix. Are you using `pdist` directly or through some other package?
Harry Richman
CC BY-SA 4.0
2023-11-01T15:34:13.097Z
This is the command I use to calculate the distance: `pd.DataFrame(squareform(pdist(kmer_matrix, 'euclidean')))` I convert it into a pandas dataframe so it's easier to output into a file and I use squareform because I read somewhere that it helps space out the final output.
rimo
CC BY-SA 4.0
2023-11-01T15:42:59.380Z
I just tried running it without `squareform` and it outputs all the distances in a descending list :/
rimo
<format-conversion><scipy><distance-matrix><megax>
Convert Symmetrix Matrix into Lower Triangular Matrix
# Convert Symmetrix Matrix into Lower Triangular Matrix

(Asked by: username_0 on 2023-10-27)

I'm using Scipy's pdist function to calculate a distance matrix which is working great but it gives me a symmetric matrix and I want to convert it into a lower-left triangular matrix.
This is what it prints out for me:
  a   b   c   d
a 0 ### ### ###
b ### 0 ### ###
c ### ### 0 ###
d ### ### ### 0

This is what I want it to be:
###
### ###
### ### ###

I know there's a function in R to do this (upper.tri) but it just replaces everything to the right of the 0 with more 0s. Is there a better/easier way to get it like this? I'm opening the file in excel and removing everything to the right with the 0 manually right now.

**Tags:** format-conversion, scipy, distance-matrix, megax
> **Comments:**
> **username_1**: From reading the scipy documentation for the [`pdist` function](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html#scipy.spatial.distance.pdist), it seems to suggest it should already only return upper-triangular entries rather than a full matrix. Are you using `pdist` directly or through some other package?  
> **username_0**: This is the command I use to calculate the distance: `pd.DataFrame(squareform(pdist(kmer_matrix, 'euclidean')))` I convert it into a pandas dataframe so it's easier to output into a file and I use squareform because I read somewhere that it helps space out the final output.  
> **username_0**: I just tried running it without `squareform` and it outputs all the distances in a descending list :/  

---

## 2 answers

### Answer by username_1 on 2023-10-27. Score: 5 | Accepted answer

NumPy has a convenient built-in function for accessing the lower-triangular or upper-trianglar entries of an array.
To get the lower-triangular entries, you can use numpy.tril_indices
to get the indices of these entries, and then use these indices to extract the entries.
import numpy as np

ar = np.array(
    [[0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]]
)
idx = np.tril_indices(3, -1)
ar[idx]
# outputs array([3, 6, 7])

This doesn't automatically produce the spacing you want, but formatting the final display is likely context-dependent.

> **Comments:**
> **username_0**: This could work for now thank you!!!  

---

### Answer by username_2 on 2023-10-27. Score: 1

There seems to be a scipy method that retains the lower triangle but with the 0s. See: <https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.tril.html#scipy.sparse.tril>

> **Comments:**
> **username_0**: Thanks! This is pretty helpful too  

---

