-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_wrappers.py
99 lines (81 loc) · 2.24 KB
/
metric_wrappers.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
"""Useful performance metric
"""
# Author: Xiaolu Xiong <[email protected]>
from abc import ABCMeta, abstractmethod
from scipy import stats
from sklearn import metrics
class MetricWrapper(object):
"""The abstract class to build performance metrics
"""
__metaclass__ = ABCMeta
@classmethod
@abstractmethod
def measure(cls, label, prediction):
"""The abstract class method to measure the performance of predictions
Parameters
----------
label : array
Lable data
prediction : array
Prediction data
Raises
------
NotImplementedError
"""
raise NotImplementedError()
class RSquare(MetricWrapper):
"""The implementation of simple coefficient of determination.
It is the square of pearson correlation.
"""
@classmethod
def measure(cls, label, prediction):
"""Measure the simple coefficient of determination.
Parameters
----------
label : array
Lable data
prediction : array
Prediction data
Returns
-------
float
The square of pearson correlation between label and prediction
"""
pearson, _ = stats.pearsonr(label, prediction)
return pearson**2
class AUC(MetricWrapper):
"""The implementation of Area under the ROC curve.
"""
@classmethod
def measure(cls, label, prediction):
"""Measure the AUC.
Parameters
----------
label : array
Lable data
prediction : array
Prediction data
Returns
-------
float
The AUC value between label and prediction
"""
return metrics.roc_auc_score(label, prediction)
class RMSE(MetricWrapper):
"""The implementation of root-mean-square error.
"""
@classmethod
def measure(cls, label, prediction):
"""Measure the RMSE
Parameters
----------
label : array
Lable data
prediction : array
Prediction data
Returns
-------
float
The RMSE value of lable and prediction
"""
return metrics.mean_squared_error(label, prediction)**0.5