10 python numpy cluster-analysis machine-learning scipy
如何在scipy
/中的相关矩阵上运行层次聚类numpy
?我有一个100行乘9列的矩阵,我想通过9个条件中每个条目的相关性进行分层聚类.我想使用1-pearson相关作为聚类的距离.假设我有一个包含100 x 9矩阵的numpy
数组X
,我该怎么做?
我尝试使用hcluster,基于这个例子:
Y=pdist(X, 'seuclidean')
Z=linkage(Y, 'single')
dendrogram(Z, color_threshold=0)
Run Code Online (Sandbox Code Playgroud)
但是,pdist
这不是我想要的,因为这是一个欧几里德距离.有任何想法吗?
谢谢.
只需将指标更改为correlation
第一行即可:
Y=pdist(X, 'correlation')
Run Code Online (Sandbox Code Playgroud)
但是,我相信代码可以简化为:
Z=linkage(X, 'single', 'correlation')
dendrogram(Z, color_threshold=0)
Run Code Online (Sandbox Code Playgroud)
因为链接会为你处理pdist.