本文主要演示pandas中DataFrame對象corr()方法的用法,該方法用來計算DataFrame對象中所有列之間的相關系數(包括pearson相關系數、Kendall Tau相關系數和spearman秩相關)。
>>> import numpy as np>>> import pandas as pd>>> df = pd.DataFrame({'A':np.random.randint(1, 100, 10), 'B':np.random.randint(1, 100, 10), 'C':np.random.randint(1, 100, 10)})>>> df A B C0 5 91 31 90 15 662 93 27 33 70 44 664 27 14 105 35 46 206 33 14 697 12 41 158 28 62 479 15 92 77>>> df.corr() # pearson相關系數 A B CA 1.000000 -0.560009 0.162105B -0.560009 1.000000 0.014687C 0.162105 0.014687 1.000000>>> df.corr('kendall') # Kendall Tau相關系數 A B CA 1.000000 -0.314627 0.113666B -0.314627 1.000000 0.045980C 0.113666 0.045980 1.000000>>> df.corr('spearman') # spearman秩相關 A B CA 1.000000 -0.419455 0.128051B -0.419455 1.000000 0.067279C 0.128051 0.067279 1.000000
以上這篇Python+pandas計算數據相關系數的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答