-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
74 lines (62 loc) · 2.09 KB
/
Copy pathevaluate.py
File metadata and controls
74 lines (62 loc) · 2.09 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
import river.metrics as rmetrics
from clustpy.metrics import unsupervised_clustering_accuracy, PairCountingScores
import numpy as np
import logging
logger = logging.getLogger(__name__)
class Purity(rmetrics.base.MultiClassMetric):
@property
def works_with_weights(self):
return False
def get(self):
purity = 0
num = 0
#print(self.cm)
for j in self.cm.classes: # predict
maxi = 0
for i in self.cm.classes: # label
num += self.cm[i][j]
if maxi < self.cm[i][j]:
maxi = self.cm[i][j]
purity += maxi
return purity / num
def getMetrics(labels, predictions):
stream_ari = rmetrics.AdjustedRand()
stream_nmi = rmetrics.NormalizedMutualInfo()
stream_ami = rmetrics.AdjustedMutualInfo()
stream_completeness = rmetrics.Completeness()
stream_fowl = rmetrics.FowlkesMallows()
stream_homogeneity = rmetrics.Homogeneity()
stream_purity = Purity()
for i in range(len(predictions)):
pred = predictions[i]
label = labels[i]
stream_ari.update(label, pred)
stream_nmi.update(label, pred)
stream_ami.update(label, pred)
stream_completeness.update(label, pred)
stream_fowl.update(label, pred)
stream_homogeneity.update(label, pred)
stream_purity.update(label, pred)
ari = stream_ari.get()
nmi = stream_nmi.get()
ami = stream_ami.get()
completeness = stream_completeness.get()
fowl = stream_fowl.get()
homogeneity = stream_homogeneity.get()
purity = stream_purity.get()
acc = unsupervised_clustering_accuracy(np.array(labels), np.array(predictions))
paircounting = PairCountingScores(np.array(labels), np.array(predictions))
f1 = paircounting.f1()
prec = paircounting.precision()
recall = paircounting.recall()
clunum = len(np.unique(predictions))
trueclunum = len(np.unique(labels))
cm = stream_purity.cm
metrics = {"accuracy": acc, "ARI": ari, "NMI": nmi, "AMI": ami, "completeness": completeness, "fowl": fowl,
"homogeneity": homogeneity, "purity": purity, "F1": f1, "precision": prec, "recall": recall,
"cluster_num": clunum}
return metrics, cm
def printMetrics(labels, predictions):
metrics, cm = getMetrics(labels, predictions)
print(cm)
print(metrics)