Python:NumPy .corrcoef()
Published Jul 24, 2024
In NumPy, the .corrcoef() method computes the Pearson correlation coefficient of two specified arrays and returns an array as the result.
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): IfTrue(default), then each row represents a variable and each column contains an observation. IfFalse, 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 libraryimport numpy as np# Defining two arraysarr1 = np.array([6, 21, 37])arr2 = np.array([1, 25, 51])# Using the .corrcoef() methodres = np.corrcoef(arr1, arr2)# Printing the resultprint(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:
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