问题
I try to create something like this: plotting results of hierarchical clustering ontop of a matrix of data in python
Unfortunatelly when I try to execute the code, I get the following warnings:
Warning (from warnings module):
File "C:\Users\USER1\Desktop\test.py", line 15
Y = sch.linkage(D, method='centroid')
ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
Warning (from warnings module):
File "C:\Users\USER1\Desktop\test.py", line 22
Y = sch.linkage(D, method='single')
ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
In addition, a small window opens but crashes ...
回答1:
That code that you linked to has a problem: it passes the square distance matrix to linkage. The first argument of linkage
has been a frequent source of confusion, so in recent versions of scipy, the code generates a warning if something that looks like a square distance matrix is passed in.
You'll have to modify your code to not pass a square distance matrix to linkage
. If you already have a such a matrix, you can convert it to the condensed form expected by linkage
with the function scipy.spatial.distance.squareform.
To avoid further confusion, I updated the code in the linked answer so that it passes a condensed distance matrix to linkage
.
来源:https://stackoverflow.com/questions/52517438/2-dendrograms-heatmap-from-condensed-correlationmatrix-with-scipy