xarray.cov#
- xarray.cov(da_a, da_b, dim=None, ddof=1)[source]#
Compute covariance between two DataArray objects along a shared dimension.
- Parameters:
da_a (
DataArray
) – Array to compute.da_b (
DataArray
) – Array to compute.dim (
str
, iterable of hashable,"..."
orNone
, optional) – The dimension along which the covariance will be computedddof (
int
, default:1
) – If ddof=1, covariance is normalized by N-1, giving an unbiased estimate, else normalization is by N.
- Returns:
covariance (
DataArray
)
See also
pandas.Series.cov
corresponding pandas function
xarray.corr
respective function to calculate correlation
Examples
>>> from xarray import DataArray >>> da_a = DataArray( ... np.array([[1, 2, 3], [0.1, 0.2, 0.3], [3.2, 0.6, 1.8]]), ... dims=("space", "time"), ... coords=[ ... ("space", ["IA", "IL", "IN"]), ... ("time", pd.date_range("2000-01-01", freq="1D", periods=3)), ... ], ... ) >>> da_a <xarray.DataArray (space: 3, time: 3)> array([[1. , 2. , 3. ], [0.1, 0.2, 0.3], [3.2, 0.6, 1.8]]) Coordinates: * space (space) <U2 'IA' 'IL' 'IN' * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 >>> da_b = DataArray( ... np.array([[0.2, 0.4, 0.6], [15, 10, 5], [3.2, 0.6, 1.8]]), ... dims=("space", "time"), ... coords=[ ... ("space", ["IA", "IL", "IN"]), ... ("time", pd.date_range("2000-01-01", freq="1D", periods=3)), ... ], ... ) >>> da_b <xarray.DataArray (space: 3, time: 3)> array([[ 0.2, 0.4, 0.6], [15. , 10. , 5. ], [ 3.2, 0.6, 1.8]]) Coordinates: * space (space) <U2 'IA' 'IL' 'IN' * time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 >>> xr.cov(da_a, da_b) <xarray.DataArray ()> array(-3.53055556) >>> xr.cov(da_a, da_b, dim="time") <xarray.DataArray (space: 3)> array([ 0.2 , -0.5 , 1.69333333]) Coordinates: * space (space) <U2 'IA' 'IL' 'IN'