-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.py
48 lines (40 loc) · 1.91 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
# -*- coding: UTF-8 -*-
# scikit-learn: 0.21.3
from sklearn import metrics
def accuracy(y_pred, y_true, mask=None, percentage=True):
"""
Calculate the multi-class classification accuracy.
:param y_pred: Predicted labels of samples. ndarray, shape: [num_samples, ].
:param y_true: Ground truth labels of samples. ndarray, shape: [num_samples, ].
:param mask: Masks of samples. 1 means valid, 0 means invalid. Optional, ndarray, shape: [num_samples, ].
:param percentage: Return accuracy as a percentage.
:return: Accuracy.
"""
acc = metrics.accuracy_score(y_true, y_pred, normalize=True, sample_weight=mask)
if percentage:
acc *= 100
return acc
def nmi(y_pred, y_true, average_method='geometric'):
"""
Calculate the normalized mutual information.
:param y_pred: Predicted labels of samples, do not need to map to the ground truth label.
ndarray, shape: [num_samples, ].
:param y_true: Ground truth labels of samples. ndarray, shape: [num_samples, ].
:param average_method: How to compute the normalizer in the denominator. Possible options
are 'min', 'geometric', 'arithmetic', and 'max'.
'min': min(U, V)
'geometric': np.sqrt(U * V)
'arithmetic': np.mean([U, V])
'max': max(U, V)
:return: Normalized mutual information.
"""
return metrics.normalized_mutual_info_score(y_true, y_pred, average_method=average_method)
def ari(y_pred, y_true):
"""
Calculate the adjusted rand index.
:param y_pred: Predicted labels of samples, do not need to map to the ground truth label.
ndarray, shape: [num_samples, ].
:param y_true: Ground truth labels of samples. ndarray, shape: [num_samples, ].
:return: Adjusted rand index.
"""
return metrics.adjusted_rand_score(y_true, y_pred)