pandas.Series.corr #

系列。corr ( other , method = 'pearson' , min_periods = None ) [来源] #

计算与其他系列的相关性,排除缺失值。

两个Series对象不需要具有相同的长度,并且在应用相关函数之前将在内部对齐。

参数
其他系列

用于计算相关性的系列。

方法{'pearson', 'kendall', 'spearman'} 或可调用

用于计算相关性的方法:

  • 皮尔逊:标准相关系数

  • kendall :Kendall Tau 相关系数

  • Spearman :斯皮尔曼等级相关

  • callable:可调用,输入两个一维数组并返回一个浮点数。

警告

请注意,从 corr 返回的矩阵沿对角线的值为 1,并且无论可调用对象的行为如何,都将是对称的。

min_periods int,可选

获得有效结果所需的最少观察次数。

返回
漂浮

与其他的相关性。

也可以看看

DataFrame.corr

计算列之间的成对相关性。

DataFrame.corrwith

计算与另一个 DataFrame 或 Series 的成对相关性。

笔记

目前,Pearson、Kendall 和 Spearman 相关性是使用成对完整观测值计算的。

自动数据对齐:与所有 pandas 操作一样,此方法会执行自动数据对齐。 corr()自动考虑具有匹配索引的值。

例子

>>> def histogram_intersection(a, b):
...     v = np.minimum(a, b).sum().round(decimals=1)
...     return v
>>> s1 = pd.Series([.2, .0, .6, .2])
>>> s2 = pd.Series([.3, .6, .0, .1])
>>> s1.corr(s2, method=histogram_intersection)
0.3

Pandas 自动将值与匹配的索引对齐

>>> s1 = pd.Series([1, 2, 3], index=[0, 1, 2])
>>> s2 = pd.Series([1, 2, 3], index=[2, 1, 0])
>>> s1.corr(s2)
-1.0