-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpearson.py
More file actions
67 lines (53 loc) · 1.91 KB
/
Copy pathpearson.py
File metadata and controls
67 lines (53 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
This will be your new module with the Pearson correlation function
First try to fill in the "pearson_1d" function.
When you are done with "pearson_1d", you should be able to run the following
command (from the day5 directory) and see no errors or failures::
python3 -m pytest test_pearson_1d.py
Then, if you are feeling brave, fill in the "pearson_2d" function. Test with::
python3 -m pytest test_pearson_2d.py
"""
import numpy as np
def pearson_1d(x, y):
""" Pearson product-moment correlation of vectors `x` and `y`
Parameters
----------
x : array shape (N,)
One-dimensional array to correlate with `y`
y : array shape (N,)
One dimensional array to correlate with `x`
Returns
-------
r_xy : scalar
Pearson product-moment correlation of vectors `x` and `y`.
"""
# Mean-center x -> mc_x
# Mean-center y -> mc_y
# a : Get sum of products of mc_x, mc_y
# b : Get sum of products of mc_x on mc_x
# c : Get sum of products of mc_y on mc_y
# return a / (sqrt(b) * sqrt(c))
# +++your code here+++
# return
def pearson_2d(x, Y):
""" Pearson product-moment correlation of vectors `x` and array `Y`
Parameters
----------
x : array shape (N,)
One-dimensional array to correlate with every column of `Y`
Y : array shape (N, P)
2D array where we correlate each column of `Y` with `x`.
Returns
-------
r_xy : array shape (P,)
Pearson product-moment correlation of vectors `x` and the columns of
`Y`, with one correlation value for every column of `Y`.
"""
# Mean-center x -> mc_x
# Mean-center every column of Y -> mc_Y
# a : Get sum of products of mc_x and every column of mc_Y
# b : Get sum of products of mc_x on mc_x
# c : Get sum of products of every column of mc_Y[:, i] on itself
# return a / (sqrt(b) * sqrt(c))
# +++your code here+++
# return