File size: 1,799 Bytes
098d2fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sklearn.metrics import accuracy_score, cohen_kappa_score, root_mean_squared_error, f1_score
import numpy as np

def arredondar_notas(notas):
    referencia = [0, 40, 80, 120, 160, 200]
    novas_notas = []
    for n in notas:
        mais_prox = 1000
        arredondado = -1
        for r in referencia:
            if abs(n - r) < mais_prox:
                arredondado = r
                mais_prox = abs(n - r)
        novas_notas.append(arredondado)
    return novas_notas

def calcular_div(notas1, notas2):
    #calcula a divergencia horizontal: duas notas são divergentes se a diferença entre elas é maior que 80
    div = 0
    for n1, n2 in zip(notas1,notas2):
        if abs(n1 - n2) > 80:
            div += 1
    return 100*div/len(notas1)

def calcular_agregado(dic_perf):
    acc = dic_perf['ACC']*100
    rmse = (200 - dic_perf['RMSE'])/2
    qwk = dic_perf['QWK']*100
    div = 100 - dic_perf['DIV']
    #print(acc, rmse, qwk, div)
    return (acc + rmse + qwk + div)/4

def calcular_resultados(y, y_hat):
    ALL_LABELS = [0, 40, 80, 120, 160, 200]
    ACC = accuracy_score(y, y_hat)
    RMSE = root_mean_squared_error(y, y_hat )
    QWK = cohen_kappa_score(y, y_hat, weights='quadratic', labels=ALL_LABELS)
    DIV = calcular_div(y, y_hat)
    macro_f1 = f1_score(
        y,
        y_hat,
        average="macro",
        labels=ALL_LABELS,
        zero_division=np.nan,
    )
    weighted_f1 = f1_score(
        y,
        y_hat,
        average="weighted",
        labels=ALL_LABELS,
        zero_division=np.nan,
    )
    if not isinstance(y, list):
        y = y.tolist()
    dic = {'ACC': ACC, 'RMSE': RMSE, 'QWK': QWK, 'DIV': DIV, 'F1-Macro': macro_f1, 'F1-Weighted': weighted_f1, 'y': y, 'y_hat': y_hat}
    dic['Agregado'] = calcular_agregado(dic)
    return dic