开发者社区> 问答> 正文

dataframe中如何得到列之间最大的相关系数?

dataframe中如何得到列之间最大的相关系数?

展开
收起
游客y244y7ln2rlpa 2021-12-05 20:30:40 368 0
1 条回答
写回答
取消 提交回答
  • df = pd.DataFrame(np.random.randint(1,100, 16).reshape(4, -1), columns=list('pqrs'), index=list('abcd'))
    # df
    print(df)
    # 得到四个列的相关系数
    abs_corrmat = np.abs(df.corr())
    print(abs_corrmat)
    # 得到每个列名与其他列的最大相关系数
    max_corr = abs_corrmat.apply(lambda x: sorted(x)[-2])
    # 显示每列与其他列的相关系数
    print('Maximum Correlation possible for each column: ', np.round(max_corr.tolist(), 2))
    
    #>	    p   q   r   s
    	a  59  99   1  34
    	b  89  60  97  40
    	c  43  35  14   6
    	d  70  59  30  53
    #>	          p         q         r         s
    	p  1.000000  0.200375  0.860051  0.744529
    	q  0.200375  1.000000  0.236619  0.438541
    	r  0.860051  0.236619  1.000000  0.341399
    	s  0.744529  0.438541  0.341399  1.000000
    
    #>	Maximum Correlation possible for each column:  [0.86 0.44 0.86 0.74]
    
    
    2021-12-05 22:22:58
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载