-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
54 lines (41 loc) · 1.37 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
from typing import Union
import pandas as pd
def accuracy(y_hat: pd.Series, y: pd.Series) -> float:
"""
Function to calculate the accuracy
"""
"""
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
return (y_hat == y).sum() / y.size
def precision(y_hat: pd.Series, y: pd.Series, cls: Union[int, str]) -> float:
"""
Function to calculate the precision
"""
assert y_hat.size == y.size
tp = ((y_hat == cls) & (y == cls)).sum()
fp = ((y_hat == cls) & (y != cls)).sum()
return tp / (tp + fp)
def recall(y_hat: pd.Series, y: pd.Series, cls: Union[int, str]) -> float:
"""
Function to calculate the recall
"""
assert y_hat.size == y.size
tp = ((y_hat == cls) & (y == cls)).sum()
fn = ((y_hat != cls) & (y == cls)).sum()
return tp / (tp + fn)
def rmse(y_hat: pd.Series, y: pd.Series) -> float:
"""
Function to calculate the root-mean-squared-error(rmse)
"""
assert y_hat.size == y.size
return ((y_hat - y) ** 2).mean() ** 0.5
def mae(y_hat: pd.Series, y: pd.Series) -> float:
"""
Function to calculate the mean-absolute-error(mae)
"""
assert y_hat.size == y.size
return (y_hat - y).abs().mean()