.corrcoef()
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.
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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours