Compute pearson product-moment correlation coefficients of two given NumPy arrays

Last Updated : 02 Sep, 2020

In NumPy, We can compute pearson product-moment correlation coefficients of two given arrays with the help of numpy.corrcoef() function.

In this function, we will pass arrays as a parameter and it will return the pearson product-moment correlation coefficients of two given arrays.

Syntax: numpy.corrcoef(x, y=None, rowvar=True, bias=, ddof=)
Return: Pearson product-moment correlation coefficients

Let’s see an example:

Example 1:

Python

 `# import library ` `import` `numpy as np ` ` `  `# create numpy 1d-array ` `array1 ``=` `np.array([``0``, ``1``, ``2``]) ` `array2 ``=` `np.array([``3``, ``4``, ``5``]) ` ` `  `# pearson product-moment correlation ` `# coefficients of the arrays ` `rslt ``=` `np.corrcoef(array1, array2) ` ` `  `print``(rslt) `

Output

```[[1. 1.]
[1. 1.]]
```

Example 2:

Python

 `# import numpy library ` `import` `numpy as np ` ` `  `# create a numpy 1d-array ` `array1 ``=` `np.array([ ``2``, ``4``, ``8``]) ` `array2 ``=` `np.array([ ``3``, ``2``,``1``]) ` ` `  ` `  `# pearson product-moment correlation ` `# coefficients of the arrays ` `rslt2 ``=` `np.corrcoef(array1, array2) ` ` `  `print``(rslt2) `

Output

```[[ 1.         -0.98198051]
[-0.98198051  1.        ]]
```

Previous
Next