本文主要演示pandas中DataFrame對象corr()方法的用法,該方法用來計(jì)算DataFrame對象中所有列之間的相關(guān)系數(shù)(包括pearson相關(guān)系數(shù)、Kendall Tau相關(guān)系數(shù)和spearman秩相關(guān))。 >>> 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 C 0 5 91 3 1 90 15 66 2 93 27 3 3 70 44 66 4 27 14 10 5 35 46 20 6 33 14 69 7 12 41 15 8 28 62 47 9 15 92 77 >>> df.corr() # pearson相關(guān)系數(shù) A B C A 1.000000 -0.560009 0.162105 B -0.560009 1.000000 0.014687 C 0.162105 0.014687 1.000000 >>> df.corr('kendall') # Kendall Tau相關(guān)系數(shù) A B C A 1.000000 -0.314627 0.113666 B -0.314627 1.000000 0.045980 C 0.113666 0.045980 1.000000 >>> df.corr('spearman') # spearman秩相關(guān) A B C A 1.000000 -0.419455 0.128051 B -0.419455 1.000000 0.067279 C 0.128051 0.067279 1.000000
|