<p>Clustering like this is typically done with scipy. Here's the code we use in deepTools (original context <a href="https://github.com/fidelram/deepTools/blob/master/deeptools/heatmapper.py#L1192-L1196" rel="noreferrer">here</a>):</p>

<pre><code>from scipy.cluster.hierarchy import fcluster, linkage
Z = linkage(some_matrix)  # You might want to set `method` and `metric`
groups = fcluster(Z, nGroups)  # You might want to set `criterion`
</code></pre>

<p><code>groups</code> is then a vector containing the group assignment of the input matrix according to the clustering. As an aside, this is exactly the same as what's shown in the last link in your post, so I'm not sure how you didn't find it.</p>

<p>BTW, at least in python, hierarchical clustering is painfully slow with large datasets.</p>

CC BY-SA 3.0
2017-10-10T18:23:00.567Z
thanks; I had indeed been wondering about Python's usual libraries for clustering, including scipy, being awfully slow for hierarchical clustering
tsttst
CC BY-SA 3.0
2017-10-10T07:33:52.200Z
2017-10-10T07:33:52.200Z
Devon Ryan
<p>Is there a good way of obtaining the labels (e.g. genes) within individual clusters that haven been clustered hierarchically in Python (preferentially, but not necessarily, by seaborn)?</p>

<p>I found these difficulties highlighted elsewhere (<a href="http://altanalyze.blogspot.com/2012/06/hierarchical-clustering-heatmaps-in.html" rel="nofollow noreferrer">here</a>, and <a href="https://stackoverflow.com/questions/42470289/get-values-included-in-each-cluster">here</a>), and would hope for some simple option (e.g.: like in MATLAB where cutoff can be specified, and corresponding genes would be returned), without potential risk that the plotted hierarchical heatmap differed from the cluster assignment ( see <a href="https://stackoverflow.com/questions/28687882/cutting-scipy-hierarchical-dendrogram-into-clusters-via-a-threshold-value">here</a>) </p>

CC BY-SA 3.0
2017-10-10T03:59:26.700Z
This question needs more information; it's not specific enough. Python is a programming language, not a toolkit. At the moment, it's similar to asking "Is there a good way to find the names of common taxi companies that are found in cities in Europe (preferentially, but not necessarily, French cities)?"
gringer
CC BY-SA 3.0
2017-10-10T05:42:54.087Z
@gringer; either solution or library would be fine as long as it can be called from Python and does the above job and works well (which would exclude for me solutions that go through Python executing compiled local code through bash, or calling local instance of R or MATLAB); perhaps: "is there at least one country in Europe, where it is possible to order a taxi (preferentially, but not necessarily, by phoning the taxi company)?"
tsttst
<python><clustering>
How to obtain clusters of hierarchical heatmap when using Python?
# How to obtain clusters of hierarchical heatmap when using Python?

(Asked by: username_0 on 2017-10-09)

Is there a good way of obtaining the labels (e.g. genes) within individual clusters that haven been clustered hierarchically in Python (preferentially, but not necessarily, by seaborn)?

I found these difficulties highlighted elsewhere ([here](http://altanalyze.blogspot.com/2012/06/hierarchical-clustering-heatmaps-in.html), and [here](https://stackoverflow.com/questions/42470289/get-values-included-in-each-cluster)), and would hope for some simple option (e.g.: like in MATLAB where cutoff can be specified, and corresponding genes would be returned), without potential risk that the plotted hierarchical heatmap differed from the cluster assignment ( see [here](https://stackoverflow.com/questions/28687882/cutting-scipy-hierarchical-dendrogram-into-clusters-via-a-threshold-value))

**Tags:** python, clustering
> **Comments:**
> **username_1**: This question needs more information; it's not specific enough. Python is a programming language, not a toolkit. At the moment, it's similar to asking "Is there a good way to find the names of common taxi companies that are found in cities in Europe (preferentially, but not necessarily, French cities)?"  
> **username_0**: @gringer; either solution or library would be fine as long as it can be called from Python and does the above job and works well (which would exclude for me solutions that go through Python executing compiled local code through bash, or calling local instance of R or MATLAB); perhaps: "is there at least one country in Europe, where it is possible to order a taxi (preferentially, but not necessarily, by phoning the taxi company)?"  

---

## 1 answers

### Answer by username_2 on 2017-10-10. Score: 6 | Accepted answer

Clustering like this is typically done with scipy. Here's the code we use in deepTools (original context here):
from scipy.cluster.hierarchy import fcluster, linkage
Z = linkage(some_matrix)  # You might want to set `method` and `metric`
groups = fcluster(Z, nGroups)  # You might want to set `criterion`

groups is then a vector containing the group assignment of the input matrix according to the clustering. As an aside, this is exactly the same as what's shown in the last link in your post, so I'm not sure how you didn't find it.
BTW, at least in python, hierarchical clustering is painfully slow with large datasets.

> **Comments:**
> **username_0**: thanks; I had indeed been wondering about Python's usual libraries for clustering, including scipy, being awfully slow for hierarchical clustering  

---

