-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metrics.py
226 lines (150 loc) · 5.54 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import numpy as np
import matplotlib.pyplot as plt
def accuracy (y, y_hat):
'''
Function to calculate accuracy
:param y : actual values
:param y_hat : predicted values
:return : accuracy
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
correct = 0
for (actual, predicted) in zip(y, y_hat):
if(actual == predicted):
correct += 1
return correct / len(y)
def __true_positive (y, y_hat, positive_value = 1):
'''
Function to calculate number of true positives
:param y : actual values
:param y_hat : predicted values
:param positive_value : value of positive sample, Default = 1
:return : true positives
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
count = 0
for (actual, predicted) in zip(y, y_hat):
if(actual == positive_value and predicted == positive_value):
count += 1
return count
def __true_negetive (y, y_hat, positive_value = 1):
'''
Function to calculate number of true negetives
:param y : actual values
:param y_hat : predicted values
:param positive_value : value of positive sample, Default = 1
:return : true negetives
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
count = 0
for (actual, predicted) in zip(y, y_hat):
if(actual != positive_value and predicted != positive_value):
count += 1
return count
def __false_positive (y, y_hat, positive_value = 1):
'''
Function to calculate number of false positives
:param y : actual values
:param y_hat : predicted values
:param positive_value : value of positive sample, Default = 1
:return : false positives
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
count = 0
for (actual, predicted) in zip(y, y_hat):
if(actual != positive_value and predicted == positive_value):
count += 1
return count
def __false_negetives (y, y_hat, positive_value = 1):
'''
Function to calculate number of false negetives
:param y : actual values
:param y_hat : predicted values
:param positive_value : value of positive sample, Default = 1
:return : false negetives
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
correct = 0
for (actual, predicted) in zip(y, y_hat):
if(actual == positive_value and predicted != positive_value):
correct += 1
return correct
def precision ( y, y_hat, positive_value = 1):
'''
Function to calculate number of true positives
:param y : actual values
:param y_hat : predicted values
:param positive_value : value of positive sample, Default = 1
:return : true positives
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
true_positive = __true_positive(y, y_hat, positive_value = positive_value)
false_positive = __false_positive(y, y_hat, positive_value = positive_value)
precision = true_positive / (true_positive + false_positive)
return precision
def recall ( y, y_hat, positive_value = 1):
'''
Function to calculate number of true positives
:param y : actual values
:param y_hat : predicted values
:param positive_value : value of positive sample, Default = 1
:return : true positives
'''
assert len(y) == len(y_hat) , "Unequal length of y and y_hat"
true_positive = __true_positive(y, y_hat, positive_value = positive_value)
false_negetives = __false_negetives(y, y_hat, positive_value = positive_value)
recall = true_positive / (true_positive + false_negetives)
return recall
def f1(y, y_hat, positive_value = 1):
"""
Function to calculate f1 score
:param y: list of true values
:param y_hat: list of predicted values
:param positive_value : value of positive sample, Default = 1
:return: f1 score
"""
p = precision(y, y_hat, positive_value= positive_value)
r = recall(y, y_hat, positive_value= positive_value)
score = 2 * p * r / (p + r)
return score
def tpr(y, y_hat, positive_value = 1):
"""
Function to calculate tpr
:param y: list of true values
:param y_hat: list of predicted values
:param positive_value : value of positive sample, Default = 1
:return: tpr/recall
"""
return recall(y, y_hat, positive_value= positive_value)
def fpr(y, y_hat, positive_value = 1):
"""
Function to calculate tpr
:param y: list of true values
:param y_hat: list of predicted values
:param positive_value : value of positive sample, Default = 1
:return: tpr/recall
"""
fp = __false_positive(y, y_hat, positive_value= positive_value)
tn = __true_negetive(y, y_hat, positive_value= positive_value)
return fp / (tn + fp)
def roc(y, y_pred, thresholds, positive_value = 1):
"""
Function to plot roc curve
:param y: list of true values
:param y_pred: list of predicted probablities
:param thresholds: list of threshold
:param positive_value : value of positive sample, Default = 1
"""
tpr_list = []
fpr_list= []
for threshold in thresholds:
y_hat = np.where(y_pred >= threshold, positive_value, 0)
tpr_val = tpr(y, y_hat, positive_value = positive_value)
fpr_val = fpr(y, y_hat, positive_value = positive_value)
tpr_list.append(tpr_val)
fpr_list.append(fpr_val)
plt.plot( fpr_list, tpr_list,)
plt.xlabel("fpr")
plt.ylabel("tpr")
plt.title("ROC Curve")
plt.show()