pandas.core.groupby.SeriesGroupBy.corr#
- SeriesGroupBy.corr(other, method='pearson', min_periods=None)[源代码]#
计算与 other Series 的相关性,排除缺失值。
两个 Series 对象不要求长度相同,它们将在应用相关函数之前在内部对齐。
- Parameters:
- otherSeries
用于计算相关性的 Series。
- <strong>method</strong>{‘pearson’, ‘kendall’, ‘spearman’} 或 callable
用于计算相关性的方法:
pearson : 标准相关系数
kendall : Kendall Tau 相关系数
spearman : Spearman 秩相关系数
callable: 带有两个一维 ndarrays 输入并返回一个浮点数的函数。
警告
请注意,corr 返回的矩阵在对角线上为 1,并且无论可调用函数的行为如何,它都将是对称的。
- min_periodsint, optional
获得有效结果所需的最小观测数。
- Returns:
- float
与 other 的相关性。
参见
DataFrame.corr计算列之间的成对相关性。
DataFrame.corrwith计算与另一个 DataFrame 或 Series 的成对相关性。
Notes
Pearson、Kendall 和 Spearman 相关性目前使用成对完整观测值进行计算。
自动数据对齐:与所有 pandas 操作一样,此方法执行自动数据对齐。
corr()自动考虑具有匹配索引的值。Examples
>>> 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