|
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): |
|
|
|
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'] |
|
|
|
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 |