-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
85 lines (65 loc) · 2.48 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
import json
import numpy as np
from scipy.optimize import linear_sum_assignment
from sklearn.metrics import adjusted_rand_score, accuracy_score, fowlkes_mallows_score, normalized_mutual_info_score, adjusted_mutual_info_score
import torch
from dtw import dtw
from scipy.spatial.distance import directed_hausdorff
import re
import pandas as pd
from tqdm import tqdm
def nmi_score(y, y_pred):
return normalized_mutual_info_score(y, y_pred)
def ami_score(y, y_pred):
return adjusted_mutual_info_score(y, y_pred)
def ari_score(y, y_pred):
return adjusted_rand_score(y, y_pred)
def fms_score(y, y_pred):
return fowlkes_mallows_score(y, y_pred)
def cluster_acc(y_true, y_pred):
"""
Calculate unsupervised clustering accuracy. Requires scikit-learn installed
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
row_ind, col_ind = linear_sum_assignment(w.max() - w)
return w[row_ind, col_ind].sum() * 1.0 / y_pred.size
def cluster_purity(y_true, y_pred):
"""
Calculate clustering purity
https://en.wikipedia.org/wiki/Cluster_analysis#Evaluation_and_assessment
# Arguments
y_true: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
purity, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
label_mapping = w.argmax(axis=1)
y_pred_voted = y_pred.copy()
for i in range(y_pred.size):
y_pred_voted[i] = label_mapping[y_pred[i]]
return accuracy_score(y_pred_voted, y_true)
def recall_at_k(predictions, true_ids, k):
recall_scores = []
for i in range(predictions.size(0)):
top_k_preds = torch.topk(predictions[i], k).indices
if true_ids[i] in top_k_preds:
recall_scores.append(1)
else:
recall_scores.append(0)
return np.mean(recall_scores)