mindspore.ops.cov
- mindspore.ops.cov(input, *, correction=1, fweights=None, aweights=None)[source]
Return the covariance matrix of the input tensor, where the rows of the input tensor represent variables and the columns represent observations. The diagonal of the covariance matrix contains the variance of each variable in the input tensor, while the off-diagonal elements represent the covariance between pairs of variables. If the input is a 0-D or 1-D tensor, its variance is returned.
The unbiased sample covariance of the variables
and is given by the following formula:where
and are the simple means of the and respectively.If fweights and/or aweights are provided, the unbiased weighted covariance is calculated, which is given by:
where
denotes fweights or aweights based on whichever is provided, or if both are provided, and is the weighted mean of the variable.Warning
The values of fweights and aweights cannot be negative, and the negative weight scene result is undefined.
Note
Currently, complex number is not supported.
- Parameters
input (Tensor) – The 0-D, 1-D or 2-D tensor.
- Keyword Arguments
correction (int, optional) – The difference between sample size and sample degrees of freedom. correction = 0 will return the simple average. Defaults to Bessel's correction, correction = 1 which returns the unbiased estimate, even if both fweights and aweights are specified.
fweights (Tensor, optional) – 0D or 1D tensor containing integer frequency weight, indicating the number of repetition of each observation vector. Its numel must equal the number of columns of input. Ignored if None. Default
None
.aweights (Tensor, optional) – A scalar or 1D Tensor containing float observation weights represents the importance of each observation vector. The higher the importance, the greater the corresponding value. Its numel must equal the number of columns of input. Must have floating point dtype. Ignored if None. Default
None
.
- Returns
Tensor
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> input = mindspore.tensor([[0., 5., 7.], ... [3., 5., 0.]]) >>> output = mindspore.ops.cov(input) >>> print(output) [[13. -3.5 ] [-3.5 6.3333335]] >>> output = mindspore.ops.cov(input, correction=0) >>> print(output) [[ 8.666667 -2.3333333] [-2.3333333 4.2222223]] >>> fw = mindspore.tensor([5, 2, 4], dtype=mindspore.int64) >>> aw = mindspore.tensor([0.4588, 0.9083, 0.7616], mindspore.float32) >>> output = mindspore.ops.cov(input, fweights=fw, aweights=aw) >>> print(output) [[10.146146 -3.47241 ] [-3.47241 4.716825]]