-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetrics.py
More file actions
139 lines (106 loc) · 4.67 KB
/
metrics.py
File metadata and controls
139 lines (106 loc) · 4.67 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
import numpy as np
def MAE(scores, targets):
MAE = F.l1_loss(scores, targets)
MAE = MAE.detach().item()
return MAE
def accuracy_TU(scores, targets):
scores = scores.detach().argmax(dim=1)
acc = (scores == targets).float().sum().item()
return acc
def accuracy_MNIST_CIFAR(scores, targets):
scores = scores.detach().argmax(dim=1)
acc = (scores == targets).float().sum().item()
return acc
def accuracy_CITATION_GRAPH(scores, targets):
scores = scores.detach().argmax(dim=1)
acc = (scores == targets).float().sum().item()
acc = acc / len(targets)
return acc
def accuracy_SBM(scores, targets):
S = targets.cpu().numpy()
C = np.argmax(torch.nn.Softmax(dim=1)(scores).cpu().detach().numpy(), axis=1)
CM = confusion_matrix(S, C).astype(np.float32)
nb_classes = CM.shape[0]
targets = targets.cpu().detach().numpy()
nb_non_empty_classes = 0
pr_classes = np.zeros(nb_classes)
for r in range(nb_classes):
cluster = np.where(targets == r)[0]
if cluster.shape[0] != 0:
pr_classes[r] = CM[r, r] / float(cluster.shape[0])
if CM[r, r] > 0:
nb_non_empty_classes += 1
else:
pr_classes[r] = 0.0
acc = 100. * np.sum(pr_classes) / float(nb_classes)
return acc
def binary_f1_score(scores, targets):
"""Computes the F1 score using scikit-learn for binary class labels.
Returns the F1 score for the positive class, i.e. labelled '1'.
"""
y_true = targets.cpu().numpy()
y_pred = scores.argmax(dim=1).cpu().numpy()
return f1_score(y_true, y_pred, average='binary')
def accuracy_VOC(scores, targets):
scores = scores.detach().argmax(dim=1).cpu()
targets = targets.cpu().detach().numpy()
acc = f1_score(scores, targets, average='weighted')
return acc
# clustering
from sklearn.metrics.cluster import contingency_matrix
def _compute_counts(y_true, y_pred): # TODO(tsitsulin): add docstring pylint: disable=missing-function-docstring
contingency = contingency_matrix(y_true, y_pred)
same_class_true = np.max(contingency, 1)
same_class_pred = np.max(contingency, 0)
diff_class_true = contingency.sum(axis=1) - same_class_true
diff_class_pred = contingency.sum(axis=0) - same_class_pred
total = contingency.sum()
true_positives = (same_class_true * (same_class_true - 1)).sum()
false_positives = (diff_class_true * same_class_true * 2).sum()
false_negatives = (diff_class_pred * same_class_pred * 2).sum()
true_negatives = total * (total - 1) - true_positives - false_positives - false_negatives
return true_positives, false_positives, false_negatives, true_negatives
def modularity(adjacency, clusters):
"""Computes graph modularity.
Args:
adjacency: Input graph in terms of its sparse adjacency matrix.
clusters: An (n,) int cluster vector.
Returns:
The value of graph modularity.
https://en.wikipedia.org/wiki/Modularity_(networks)
"""
degrees = adjacency.sum(axis=0).A1
n_edges = degrees.sum() # Note that it's actually 2*n_edges.
result = 0
for cluster_id in np.unique(clusters):
cluster_indices = np.where(clusters == cluster_id)[0]
adj_submatrix = adjacency[cluster_indices, :][:, cluster_indices]
degrees_submatrix = degrees[cluster_indices]
result += np.sum(adj_submatrix) - (np.sum(degrees_submatrix) ** 2) / n_edges
return result / n_edges
def precision(y_true, y_pred):
true_positives, false_positives, _, _ = _compute_counts(y_true, y_pred)
return true_positives / (true_positives + false_positives)
def recall(y_true, y_pred):
true_positives, _, false_negatives, _ = _compute_counts(y_true, y_pred)
return true_positives / (true_positives + false_negatives)
def accuracy_score(y_true, y_pred):
true_positives, false_positives, false_negatives, true_negatives = _compute_counts(
y_true, y_pred)
return (true_positives + true_negatives) / (true_positives + false_positives + false_negatives + true_negatives)
def conductance(adjacency, clusters): # TODO(tsitsulin): add docstring pylint: disable=missing-function-docstring
inter = 0
intra = 0
cluster_idx = np.zeros(adjacency.shape[0], dtype=bool)
for cluster_id in np.unique(clusters):
cluster_idx[:] = 0
cluster_idx[np.where(clusters == cluster_id)[0]] = 1
adj_submatrix = adjacency[cluster_idx, :]
inter += np.sum(adj_submatrix[:, cluster_idx])
intra += np.sum(adj_submatrix[:, ~cluster_idx])
return intra / (inter + intra)