Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.ops.cov

View Source On Gitee
mindspore.ops.cov(input, *, correction=1, fweights=None, aweights=None)[source]

Given the input and weights, returns the covariance matrix (the square matrix of the covariance of each pair of variables) of input, where the input row is the variable and the column is the observation value.

The diagonal contains each variable and its own covariance. If input is a scalar or 1D vector of a single variable, its variance will be returned.

The unbiased sample covariance of the variables a and b is given by the following formula:

covw(a,b)=i=1N(aia¯)(bib¯)N  1

where a¯ and b¯ are the simple means of the a and b respectively.

If fweights and/or aweights are provided, the unbiased weighted covariance is calculated, which is given by:

covw(a,b)=i=1Nwi(aiμa)(biμb)i=1Nwi  1

where w denotes fweights or aweights based on whichever is provided, or w=fweights×aweights if both are provided, and μx=i=1Nwixii=1Nwi 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) – A 2D matrix, or a scalar or 1D vector of a single variable

Keyword Arguments
  • correction (int, optional) – The difference between sample size and sample degrees of freedom. Defaults to Bessel's correction, correction = 1 which returns the unbiased estimate, even if both fweights and aweights are specified. correction = 0 will return the simple average. Default: 1 .

  • fweights (Tensor, optional) – Scalar or one-dimensional 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, the covariance matrix Tensor of input.

Raises
  • ValueError – If the dimensions of input is greater than 2.

  • ValueError – If the dimensions of fweights is greater than 1.

  • ValueError – If the numel of fweights not equal the number of columns of input.

  • ValueError – If the numel of aweights not equal the number of columns of input.

  • ValueError – If the dimensions of aweights is greater than 1.

  • TypeError – If the dtype of input is bool.

  • TypeError – If the dtype of fweights is not an integer type.

  • TypeError – If the dtype of aweights is not a floating point type.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import ops
>>> x = ms.Tensor([[0., 3.], [5., 5.], [7., 0.]]).T
>>> print(x)
[[0. 5. 7.]
 [3. 5. 0.]]
>>> print(ops.cov(x))
[[13.        -3.5      ]
 [-3.5        6.3333335]]
>>> print(ops.cov(x, correction=0))
[[ 8.666667  -2.3333333]
 [-2.3333333  4.2222223]]
>>> fw = ms.Tensor([5, 2, 4], dtype=ms.int64)
>>> aw = ms.Tensor([0.4588, 0.9083, 0.7616], ms.float32)
>>> print(ops.cov(x, fweights=fw, aweights=aw))
[[10.146146 -3.47241 ]
 [-3.47241   4.716825]]