Skip to content

Commit 86d7b1e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a5fa33e commit 86d7b1e

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

machine_learning/pearson_correlation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import numpy as np
22

3+
34
def pearson_correlation(data_x: np.ndarray, data_y: np.ndarray) -> float:
45
"""
56
Calculate the Pearson correlation coefficient between two sets of data.
67
78
Parameters:
8-
data_x (np.ndarray): Array of numeric values representing a column of data
9+
data_x (np.ndarray): Array of numeric values representing a column of data
910
that will be compared with another column to determine
1011
how strongly the two vectors are related.
11-
data_y (np.ndarray): Array of numeric values representing the second column
12+
data_y (np.ndarray): Array of numeric values representing the second column
1213
of data to compare with data_x.
1314
1415
Returns:
@@ -25,7 +26,7 @@ def pearson_correlation(data_x: np.ndarray, data_y: np.ndarray) -> float:
2526
"""
2627
if len(data_x) != len(data_y):
2728
raise ValueError("data_x and data_y must have the same length")
28-
29+
2930
n = len(data_x)
3031
if n == 0:
3132
return 0.0
@@ -34,7 +35,9 @@ def pearson_correlation(data_x: np.ndarray, data_y: np.ndarray) -> float:
3435
mean_y = np.mean(data_y)
3536

3637
numerator = np.sum((data_x - mean_x) * (data_y - mean_y))
37-
denominator = np.sqrt(np.sum((data_x - mean_x)**2) * np.sum((data_y - mean_y)**2))
38+
denominator = np.sqrt(
39+
np.sum((data_x - mean_x) ** 2) * np.sum((data_y - mean_y) ** 2)
40+
)
3841

3942
if denominator == 0:
4043
return 0.0
@@ -44,4 +47,5 @@ def pearson_correlation(data_x: np.ndarray, data_y: np.ndarray) -> float:
4447

4548
if __name__ == "__main__":
4649
import doctest
50+
4751
doctest.testmod()

0 commit comments

Comments
 (0)