GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy: Compute pearson product-moment correlation coefficients of two given arrays - w3resource

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

Last update on May 05 2025 11:15:49 (UTC/GMT +8 hours)


10. Pearson Product-Moment Correlation Coefficients

Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating an array 'x' containing elements [0, 1, 3]
x = np.array([0, 1, 3])

# Creating an array 'y' containing elements [2, 4, 5]
y = np.array([2, 4, 5])

# Displaying the original array 'x'
print("\nOriginal array1:")
print(x)

# Displaying the original array 'y'
print("\nOriginal array1:")
print(y)

# Calculating the Pearson product-moment correlation coefficients of arrays 'x' and 'y' using np.corrcoef()
print("\nPearson product-moment correlation coefficients of the said arrays:\n", np.corrcoef(x, y))

Sample Output:

Original array1:
[0 1 3]

Original array1:
[2 4 5]

Pearson product-moment correlation coefficients of the said arrays:
 [[1.         0.92857143]
 [0.92857143 1.        ]]

Explanation:

In the above code –

x = np.array([0, 1, 3]): This creates a NumPy array x with values [0, 1, 3].

y = np.array([2, 4, 5]): This creates a NumPy array y with values [2, 4, 5].

print(np.corrcoef(x, y)): This computes the correlation matrix between x and y, and prints it to the console.


For more Practice: Solve these Related Problems:

  • Write a function that computes the Pearson correlation coefficient matrix of two 1D arrays using np.corrcoef.
  • Create a program that calculates the Pearson coefficient manually and compares it with the result from np.corrcoef.
  • Implement a solution that computes Pearson correlation for multi-dimensional arrays along a specified axis.
  • Develop a method that tests Pearson correlation on datasets with known relationships to verify the coefficient’s accuracy.

Go to:

  • NumPy Statistics Exercises Home ↩
  • NumPy Exercises Home ↩

PREV : Cross-Correlation of Two Arrays
NEXT : Element-wise Finite/Infinite/NaN Checks

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.