-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
125 lines (119 loc) · 2.76 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import pandas as pd
import numpy as np
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.
"""
y_hat = pd.Series(y_hat)
assert(y_hat.size == y.size)
y_hat = list(y_hat)
y = list(y)
l = len(y)
match = 0
for j in range(l):
if (y[j]==y_hat[j]):
match +=1
ans = match/l
return ans
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
"""
y_hat = pd.Series(y_hat)
assert(y_hat.size == y.size)
l = len(y)
y_hat = list(y_hat)
match = 0
y = list(y)
deno = 0
for j in range(l):
if (y[j]==y_hat[j]) and (y_hat[j]==cls):
match += 1
if (y_hat[j]==cls):
deno += 1
if deno==0:
ans = 1
else:
ans = match/deno
return ans
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
"""
y_hat = pd.Series(y_hat)
assert(y_hat.size == y.size)
l = len(y)
y_hat = list(y_hat)
y = list(y)
match = 0
deno = 0
for j in range(l):
if (y[j]==y_hat[j]) and (y_hat[j]==cls):
match += 1
if (y[j]==cls):
deno += 1
if deno==0:
ans = 1
else:
ans = match/deno
return ans
def rmse(y_hat, y):
"""
Function to calculate the root-mean-squared-error(rmse)
Inputs:
> y_hat: pd.Series of predictions
> y: pd.Series of ground truth
Output:
> Returns the rmse as float
"""
y_hat = pd.Series(y_hat)
assert(y_hat.size == y.size)
sm = 0.0
l = len(y)
# print(l)
for j in range(l):
sm += (y_hat[j]-y[j])**2
# print(sm)
mse = sm/float(l)
ans = np.sqrt(mse)
# print(ans)
return ans
def mae(y_hat, y):
"""
Function to calculate the mean-absolute-error(mae)
Inputs:
> y_hat: pd.Series of predictions
> y: pd.Series of ground truth
Output:
> Returns the mae as float
"""
y_hat = pd.Series(y_hat)
assert(y_hat.size == y.size)
sm = 0.0
l = len(y)
for j in range(l):
sm += np.absolute(y_hat[j]-y[j])
ans = sm/float(l)
return ans