This Python script calculates the Pearson correlation coefficient between two sets of values, x
and y
. The Pearson correlation coefficient is a measure of the linear relationship between two variables, where a value of 1 indicates a perfect positive correlation, -1 indicates a perfect negative correlation, and 0 indicates no correlation.
-
The script accepts two lists of integers (values for
x
andy
). -
It calculates the Pearson correlation coefficient using the following formula:
r = [ n(Σxy) - (Σx)(Σy) ] / sqrt[ (nΣx² - (Σx)²)(nΣy² - (Σy)²) ]
-
The result is printed as the Pearson correlation coefficient.
- Python 3.x
- Clone or download this repository.
- Run the script with any Python environment.
- The script will prompt for input values.
python pearson_correlation.py
Input:
Enter the values of x:
1 2 3 4 5
Enter the values of y:
2 4 6 8 10
Output:
Pearson correlation coefficient: 1.0
- If the denominator in the Pearson formula is zero, the script will handle this gracefully by setting the result to 0.
This project is licensed under the MIT License.