Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.corrcoef function

PyTorch torch.corrcoef function


PyTorch torch reference manual 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

]

]

)

print

(

"Input matrix shape:"

,

x.

shape

)

print

(

"Input:"

)

print

(

x

)

Compute the correlation coefficient matrix

corr

=

torch.

corrcoef

(

x

)

print

(

"Correlation coefficient matrix:"

)

print

(

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

)

print

(

"Correlation coefficient of a and b:"

,

corr2

[

0

,

1

]

)

The output is close to 1, indicating a strong positive correlation


PyTorch torch reference manual PyTorch torch reference manual