cov

paddle.linalg. cov ( x, rowvar=True, ddof=True, fweights=None, aweights=None, name=None ) [source]

Estimate the covariance matrix of the input variables, given data and weights.

A covariance matrix is a square matrix, indicate the covariance of each pair variables in the input matrix. For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the covariance matrix element Cij is the covariance of xi and xj. The element Cii is the variance of xi itself.

Parameters
  • x (Tensor) – A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below.

  • rowvar (Bool, optional) – If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True.

  • ddof (Bool, optional) – If ddof=True will return the unbiased estimate, and ddof=False will return the simple average. Default: True.

  • fweights (Tensor, optional) – 1-D Tensor of integer frequency weights; The number of times each observation vector should be repeated. Default: None.

  • aweights (Tensor, optional) – 1-D Tensor of observation vector weights. How important of the observation vector, larger data means this element is more important. Default: None.

  • name (str, optional) – Name of the output. Default is None. It’s used to print debug info for developers. Details: Name .

Returns

The covariance matrix Tensor of the variables.

Return type

Tensor

Examples

>>> import paddle
>>> paddle.seed(2023)

>>> xt = paddle.rand((3, 4))
>>> paddle.linalg.cov(xt)
>>> print(xt)
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.86583614, 0.52014720, 0.25960937, 0.90525323],
 [0.42400089, 0.40641287, 0.97020894, 0.74437362],
 [0.51785129, 0.73292869, 0.97786582, 0.04315904]])