xskillscore.pearson_r

xskillscore.pearson_r(a, b, dim=None, weights=None, skipna=False, keep_attrs=False)

Pearson’s correlation coefficient.

\[r_{ab} = \frac{ \sum_{i=i}^{n} (a_{i} - \bar{a}) (b_{i} - \bar{b}) } {\sqrt{ \sum_{i=1}^{n} (a_{i} - \bar{a})^{2} } \sqrt{ \sum_{i=1}^{n} (b_{i} - \bar{b})^{2} }}\]
Parameters:
  • a (xarray.Dataset or xarray.DataArray) – Labeled array(s) over which to apply the function.

  • b (xarray.Dataset or xarray.DataArray) – Labeled array(s) over which to apply the function.

  • dim (str, list) – The dimension(s) to apply the correlation along. Note that this dimension will be reduced as a result. Defaults to None reducing all dimensions.

  • weights (xarray.Dataset or xarray.DataArray or None) – Weights matching dimensions of dim to apply during the function.

  • skipna (bool) – If True, skip NaNs when computing function.

  • keep_attrs (bool) – If True, the attributes (attrs) will be copied from the first input to the new one. If False (default), the new object will be returned without attributes.

Returns:

Pearson’s correlation coefficient.

Return type:

xarray.DataArray or xarray.Dataset

References

https://en.wikipedia.org/wiki/Pearson_correlation_coefficient

Examples

>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.pearson_r(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[-0.17455755, -0.26648379, -0.74265833],
       [ 0.32535918,  0.42496646,  0.1940647 ],
       [-0.3203094 ,  0.33207755,  0.89250429]])
Dimensions without coordinates: x, y