DataFrame.corr(method='pearson', min_periods=1)
參數說明:
method:可選值為{‘pearson’, ‘kendall’, ‘spearman’}
pearson:Pearson相關系數來衡量兩個數據集合是否在一條線上面,即針對線性數據的相關系數計算,針對非線性 數據便會有誤差。
kendall:用於反映分類變量相關性的指標,即針對無序序列的相關系數,非正太分布的數據
spearman:非線性的,非正太分析的數據的相關系數
min_periods:樣本最少的數據量
返回值:各類型之間的相關系數DataFrame表格。
為區分不同參數之間的區別,我們實驗如下:
from pandas import DataFrame import pandas as pd x=[a for a in range(100)] #構造一元二次方程,非線性關系 def y_x(x): return 2*x**2+4 y=[y_x(i) for i in x] data=DataFrame({'x':x,'y':y}) #查看下data的數據結構 data.head() Out[34]: x y 0 0 4 1 1 6 2 2 12 3 3 22 4 4 36 data.corr() Out[35]: x y x 1.000000 0.967736 y 0.967736 1.000000 data.corr(method='spearman') Out[36]: x y x 1.0 1.0 y 1.0 1.0 data.corr(method='kendall') Out[37]: x y x 1.0 1.0 y 1.0 1.0
因為y經由函數構造出來,x和y的相關系數為1,但從實驗結構可知pearson系數,針對非線性數據有一定的誤差。
轉自:https://blog.csdn.net/walking_visitor/article/details/85128461