本文介绍了在Python中使用相关矩阵创建聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所有,我有21个行业领域的相关矩阵.现在,我想将这21个扇区分为4或5个组,将行为相似的扇区组合在一起.
all, I have a correlation matrix of 21 industry sectors. Now I want to split these 21 sectors into 4 or 5 groups, with sectors of similar behaviors grouped together.
请专家为我提供一些如何在Python中执行此操作的启发?提前谢谢!
Can experts shed me some lights on how to do this in Python please? Thanks much in advance!
推荐答案
您可能会探索使用熊猫 DataFrame.corr
和 scipy.cluster
层次化群集程序包
You might explore the use of Pandas DataFrame.corr
and the scipy.cluster
Hierarchical Clustering package
import pandas as pd
import scipy.cluster.hierarchy as spc
df = pd.DataFrame(my_data)
corr = df.corr().values
pdist = spc.distance.pdist(corr)
linkage = spc.linkage(pdist, method='complete')
idx = spc.fcluster(linkage, 0.5 * pdist.max(), 'distance')
这篇关于在Python中使用相关矩阵创建聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!