File size: 1,192 Bytes
75a56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from sklearn.metrics import accuracy_score, f1_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score
import json
import pandas as pd

def evaluation_metrics(y_true, classes, predicted_test):
    '''Calculate evaluation metrics '''

    accuracy = accuracy_score(y_true, classes)
    print(f'Accuracy: {accuracy}')

    cnf_matrix = confusion_matrix(y_true, classes)

    FP = cnf_matrix.sum(axis=0) - np.diag(cnf_matrix)  
    FN = cnf_matrix.sum(axis=1) - np.diag(cnf_matrix)
    TP = np.diag(cnf_matrix)
    TN = cnf_matrix.sum() - (FP + FN + TP)

    FP = FP.astype(float)
    FN = FN.astype(float)
    TP = TP.astype(float)
    TN = TN.astype(float)

    # true positive rate - TPR
    TPR = TP/(TP+FN)
    print(f'TPR: {np.mean(TPR)}')

    # false positive rate - FPR
    FPR = FP/(FP+TN)
    print(f'FPR: {np.mean(FPR)}')

    # F1 Score
    f1 = f1_score(y_true, classes, average='weighted')
    print(f'F1 score: {f1}')

    return (accuracy, np.mean(TPR), np.mean(FPR), f1)
    
def load_config(config_file_path):
    with open(config_file_path, 'r') as config_file:
        config = json.load(config_file)
    return config