Skip to content
Related Articles

Related Articles

Compute pearson product-moment correlation coefficients of two given NumPy arrays
  • Last Updated : 02 Sep, 2020
GeeksforGeeks - Summer Carnival Banner

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.        ]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :