adding Utils
Browse files
Utils.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.metrics import accuracy_score, cohen_kappa_score, root_mean_squared_error, f1_score
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def arredondar_notas(notas):
|
| 5 |
+
referencia = [0, 40, 80, 120, 160, 200]
|
| 6 |
+
novas_notas = []
|
| 7 |
+
for n in notas:
|
| 8 |
+
mais_prox = 1000
|
| 9 |
+
arredondado = -1
|
| 10 |
+
for r in referencia:
|
| 11 |
+
if abs(n - r) < mais_prox:
|
| 12 |
+
arredondado = r
|
| 13 |
+
mais_prox = abs(n - r)
|
| 14 |
+
novas_notas.append(arredondado)
|
| 15 |
+
return novas_notas
|
| 16 |
+
|
| 17 |
+
def calcular_div(notas1, notas2):
|
| 18 |
+
#calcula a divergencia horizontal: duas notas são divergentes se a diferença entre elas é maior que 80
|
| 19 |
+
div = 0
|
| 20 |
+
for n1, n2 in zip(notas1,notas2):
|
| 21 |
+
if abs(n1 - n2) > 80:
|
| 22 |
+
div += 1
|
| 23 |
+
return 100*div/len(notas1)
|
| 24 |
+
|
| 25 |
+
def calcular_agregado(dic_perf):
|
| 26 |
+
acc = dic_perf['ACC']*100
|
| 27 |
+
rmse = (200 - dic_perf['RMSE'])/2
|
| 28 |
+
qwk = dic_perf['QWK']*100
|
| 29 |
+
div = 100 - dic_perf['DIV']
|
| 30 |
+
#print(acc, rmse, qwk, div)
|
| 31 |
+
return (acc + rmse + qwk + div)/4
|
| 32 |
+
|
| 33 |
+
def calcular_resultados(y, y_hat):
|
| 34 |
+
ALL_LABELS = [0, 40, 80, 120, 160, 200]
|
| 35 |
+
ACC = accuracy_score(y, y_hat)
|
| 36 |
+
RMSE = root_mean_squared_error(y, y_hat )
|
| 37 |
+
QWK = cohen_kappa_score(y, y_hat, weights='quadratic', labels=ALL_LABELS)
|
| 38 |
+
DIV = calcular_div(y, y_hat)
|
| 39 |
+
macro_f1 = f1_score(
|
| 40 |
+
y,
|
| 41 |
+
y_hat,
|
| 42 |
+
average="macro",
|
| 43 |
+
labels=ALL_LABELS,
|
| 44 |
+
zero_division=np.nan,
|
| 45 |
+
)
|
| 46 |
+
weighted_f1 = f1_score(
|
| 47 |
+
y,
|
| 48 |
+
y_hat,
|
| 49 |
+
average="weighted",
|
| 50 |
+
labels=ALL_LABELS,
|
| 51 |
+
zero_division=np.nan,
|
| 52 |
+
)
|
| 53 |
+
if not isinstance(y, list):
|
| 54 |
+
y = y.tolist()
|
| 55 |
+
dic = {'ACC': ACC, 'RMSE': RMSE, 'QWK': QWK, 'DIV': DIV, 'F1-Macro': macro_f1, 'F1-Weighted': weighted_f1, 'y': y, 'y_hat': y_hat}
|
| 56 |
+
dic['Agregado'] = calcular_agregado(dic)
|
| 57 |
+
return dic
|