Hierarchical clustering of heatmap in python

好久不见. 提交于 2019-12-24 14:49:52

问题


I have a NxM matri with values that range from 0 to 20. I easily get an heatmap by using Matplotlib and pcolor. Now I'd like to apply a hierarchical clustering and a dendogram using scipy. I'd like to re-order each dimension (rows and columns) in order to show which element are similar (according to the clustering result). If the matrix would be square (NxN) the code would be something like:

clustering = linkage(matrix, method="average")
dendrogram(clustering, orientation='right')

How can I obtain the linkage matrix when the dimensions are different? Do I have to calculate manually each distance between every pair of elements?


回答1:


I'm not sure how to do it using Matplotlib and pcolor, however the seaborn packages has native support for what you're trying to accomplish.

http://seaborn.pydata.org/generated/seaborn.clustermap.html

Example:

import seaborn as sns; sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
g = sns.clustermap(flights)

Example clustergram



来源:https://stackoverflow.com/questions/26430742/hierarchical-clustering-of-heatmap-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!