Dendrogram using pandas and scipy

前端 未结 1 1527
清歌不尽
清歌不尽 2021-02-10 04:59

I wish to generate a dendrogram based on correlation using pandas and scipy. I use a dataset (as a DataFrame) consisting of returns, whic

相关标签:
1条回答
  • 2021-02-10 06:03

    Found the solution. If you have already calculated a distance matrix (be it correlation or whatever), you simply have to condense the matrix using distance.squareform. That is,

    dataframe = pd.DataFrame(data=random_returns, index=dates)
    corr = 1 - dataframe.corr() 
    
    corr_condensed = hc.distance.squareform(corr) # convert to condensed
    z = hc.linkage(corr_condensed, method='average')
    dendrogram = hc.dendrogram(z, labels=corr.columns)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题