Python:NumPy .corrcoef()

Sriparno08's avatar
Published Jul 24, 2024
Contribute to Docs

In NumPy, the .corrcoef() method computes the Pearson correlation coefficient of two specified arrays and returns an array as the result.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

numpy.corrcoef(x, y=None, rowvar=True, dtype=None)
  • x: The first array to be used for computing the Pearson correlation coefficient.
  • y (Optional): The second array to be used for computing the Pearson correlation coefficient.
  • rowvar (Optional): If True (default), then each row represents a variable and each column contains an observation. If False, then the roles are reversed.
  • dtype (Optional): Specifies the return data type.

Example

The following example demonstrates the usage of the .corrcoef() method:

# Importing the NumPy library
import numpy as np
# Defining two arrays
arr1 = np.array([6, 21, 37])
arr2 = np.array([1, 25, 51])
# Using the .corrcoef() method
res = np.corrcoef(arr1, arr2)
# Printing the result
print(res)

The above code produces the following output:

[[1. 0.99999002]
[0.99999002 1. ]]

Codebyte Example

Run the following codebyte example to understand the use of the .corrcoef() method:

Code
Output

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours