-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
77 lines (62 loc) · 1.71 KB
/
metrics.py
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
68
69
70
71
72
73
74
75
76
77
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
def rmse(y_hat, y):
return np.sqrt(np.mean(np.square(y_hat-y)))
def accuracy(y_hat, y):
"""
Function to calculate the accuracy
Inputs:
> y_hat: pd.Series of predictions
> y: pd.Series of ground truth
Output:
> Returns the accuracy as float
"""
"""
The following assert checks if sizes of y_hat and y are equal.
Students are required to add appropriate assert checks at places to
ensure that the function does not fail in corner cases.
"""
assert(y_hat.size == y.size)
assert(y_hat.size > 0)
# TODO: Write here
return (y_hat == y).sum()/y.size
def precision(y_hat, y, cls):
"""
Function to calculate the precision
Inputs:
> y_hat: pd.Series of predictions
> y: pd.Series of ground truth
> cls: The class chosen
Output:
> Returns the precision as float
"""
assert(y_hat.size == y.size)
assert(y_hat.size > 0)
pred_pos = y_hat == cls
if sum(pred_pos) > 0:
return (y_hat[pred_pos] == y[pred_pos]).sum()/pred_pos.sum()
else:
return None
def recall(y_hat, y, cls):
"""
Function to calculate the recall
Inputs:
> y_hat: pd.Series of predictions
> y: pd.Series of ground truth
> cls: The class chosen
Output:
> Returns the recall as float
"""
assert(y_hat.size == y.size)
assert(y_hat.size > 0)
act_pos = y == cls
if sum(act_pos) > 0:
return (y_hat[act_pos] == y[act_pos]).sum()/act_pos.sum()
else:
return None
def confusion_mat(y_hat, y):
assert(len(y_hat) == len(y))
y_hat = y_hat > 0.5
c = confusion_matrix(y, y_hat)
print(c)