I'm looking for a way to perform clustering separately on matrix rows and than on its columns, reorder the data in the matrix to reflect the clustering and putting it all together. The clustering problem is easily solvable, so is the dendrogram creation (for example in this blog or in "Programming collective intelligence"). However, how to reorder the data remains unclear for me.
Eventually, I'm looking for a way of creating graphs similar to the one below using naive Python (with any "standard" library such as numpy, matplotlib etc, but without using R or other external tools).
dendogram http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/r/heatmap/no_scaling.png
Clarifications
I was asked what I meant by reordering. When you cluster data in a matrix first by matrix rows, then by its columns, each matrix cell can be identified by the position in the two dendrograms. If you reorder the rows and the columns of the original matrix such that the elements that are close each to another in the dendrograms become close each to another in the matrix, and then generate heatmap, the clustering of the data may become evident to the viewer (as in the figure above)
I know this is very late to the game, but I made a plotting object based on the code from the post on this page. It's registered on pip, so to install you just have to call
check out the project's github page here : https://github.com/themantalope/pydendroheatmap
I'm not sure completely understand, but it appears you are trying to re-index each axis of the array based on sorts of the dendrogram indicies. I guess that assumes there is some comparative logic in each branch delineation. If this is the case then would this work(?):
x_idxs
andy_idxs
are the dendrogram indicies.a
is the unsorted matrix.xi
andyi
are your new row/column array indicies.a2
is the sorted matrix whilex_idxs2
andy_idxs2
are the new, sorted dendrogram indicies. This assumes that when the dendrogram was created that a0
branch column/row is always comparatively larger/smaller than a1
branch.If your y_idxs and x_idxs are not lists but are numpy arrays, then you could use
np.argsort
in a similar manner.See my recent answer, copied in part below, to this related question.
Dendrogram and distance matrix http://up.stevetjoa.com/dendrogram.png