PyTorch torch.corrcoef function
PyTorch torch reference manual
torch.corrcoef is a PyTorch function for computing the Pearson correlation coefficient matrix. It returns the correlation coefficient matrix of the input tensor, used to measure the linear correlation between variables.
Function definition
torch.corrcoef(input)
Usage examples
Example
import
torch
Compute the correlation coefficient matrix
x
=
torch.
tensor
(
[
[
1
,
2
,
3
,
4
]
,
[
2
,
3
,
4
,
5
]
,
[
3
,
4
,
5
,
6
]
]
)
(
"Input matrix shape:"
,
x.
shape
)
(
"Input:"
)
(
x
)
Compute the correlation coefficient matrix
corr
=
torch.
corrcoef
(
x
)
(
"Correlation coefficient matrix:"
)
(
corr
)
Correlation between two variables
a
=
torch.
tensor
(
[
1
,
2
,
3
,
4
,
5
]
)
b
=
torch.
tensor
(
[
2
,
4
,
6
,
8
,
10
]
)
The shape needs to be (2, n)
data
=
torch.
stack
(
[
a
,
b
]
)
corr2
=
torch.
corrcoef
(
data
)
(
"Correlation coefficient of a and b:"
,
corr2
[
0
,
1
]
)