test_models.py 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
"""
    Evaluating optimized models with test data
"""

# Libraries
# --------------------------------------------------------------------------------------------------------
import pandas as pd
import numpy as np
from xgboost import XGBClassifier
from sklearn.metrics import confusion_matrix
11
from sklearn.metrics import f1_score, make_scorer, precision_score, recall_score, accuracy_score, roc_auc_score, average_precision_score
12 13 14 15 16
from sklearn.ensemble import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
from sklearn.linear_model import  LogisticRegression
from sklearn.tree import DecisionTreeClassifier
17 18 19
from sklearn.metrics import RocCurveDisplay, roc_curve
from sklearn.metrics import PrecisionRecallDisplay, precision_recall_curve
import matplotlib.pyplot as plt
20
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
21 22 23 24 25 26 27 28 29 30 31
# --------------------------------------------------------------------------------------------------------

# Reading test data
# --------------------------------------------------------------------------------------------------------
def read_test_data():
    # Load test data
    X_test_pre = np.load('../gen_train_data/data/output/pre/X_test_pre.npy', allow_pickle=True)
    y_test_pre = np.load('../gen_train_data/data/output/pre/y_test_pre.npy', allow_pickle=True)
    X_test_post = np.load('../gen_train_data/data/output/post/X_test_post.npy', allow_pickle=True)
    y_test_post = np.load('../gen_train_data/data/output/post/y_test_post.npy', allow_pickle=True)

Joaquin Torres's avatar
Joaquin Torres committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    # Load ORIGINAL training data
    X_train_pre = np.load('../gen_train_data/data/output/pre/X_train_pre.npy', allow_pickle=True)
    y_train_pre = np.load('../gen_train_data/data/output/pre/y_train_pre.npy', allow_pickle=True)
    X_train_post = np.load('../gen_train_data/data/output/post/X_train_post.npy', allow_pickle=True)
    y_train_post = np.load('../gen_train_data/data/output/post/y_train_post.npy', allow_pickle=True)

    # Load oversampled training data
    X_train_over_pre = np.load('../gen_train_data/data/output/pre/X_train_over_pre.npy', allow_pickle=True)
    y_train_over_pre = np.load('../gen_train_data/data/output/pre/y_train_over_pre.npy', allow_pickle=True)
    X_train_over_post = np.load('../gen_train_data/data/output/post/X_train_over_post.npy', allow_pickle=True)
    y_train_over_post = np.load('../gen_train_data/data/output/post/y_train_over_post.npy', allow_pickle=True)

    # Load undersampled training data
    X_train_under_pre = np.load('../gen_train_data/data/output/pre/X_train_under_pre.npy', allow_pickle=True)
    y_train_under_pre = np.load('../gen_train_data/data/output/pre/y_train_under_pre.npy', allow_pickle=True)
    X_train_under_post = np.load('../gen_train_data/data/output/post/X_train_under_post.npy', allow_pickle=True)
    y_train_under_post = np.load('../gen_train_data/data/output/post/y_train_under_post.npy', allow_pickle=True)

50 51 52 53 54
    data_dic = {
        "X_test_pre": X_test_pre,
        "y_test_pre": y_test_pre,
        "X_test_post": X_test_post,
        "y_test_post": y_test_post,
Joaquin Torres's avatar
Joaquin Torres committed
55 56 57 58 59 60 61 62 63 64 65 66
        "X_train_pre": X_train_pre,
        "y_train_pre": y_train_pre,
        "X_train_post": X_train_post,
        "y_train_post": y_train_post,
        "X_train_over_pre": X_train_over_pre,
        "y_train_over_pre": y_train_over_pre,
        "X_train_over_post": X_train_over_post,
        "y_train_over_post": y_train_over_post,
        "X_train_under_pre": X_train_under_pre,
        "y_train_under_pre": y_train_under_pre,
        "X_train_under_post": X_train_under_post,
        "y_train_under_post": y_train_under_post,
67 68 69 70 71 72 73 74 75 76 77 78 79
    }

    return data_dic
# --------------------------------------------------------------------------------------------------------

# Returning tuned models for each situation
# --------------------------------------------------------------------------------------------------------
def get_tuned_models(group_id, method_id):
    # 1. PRE
    if group_id == 0:
        # 1.1) Trained with original dataset
        if method_id == 0:
            tuned_models = {
80 81 82 83 84 85 86 87
            "DT" : DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'sqrt', 'criterion': 'entropy'}), 
            "RF" : RandomForestClassifier(**{'criterion': 'entropy', 'max_features': 'sqrt', 'n_estimators': 123}), 
            "Bagging" : BaggingClassifier(**{'max_features': 1.0, 'max_samples': 0.8, 'n_estimators': 13, 'warm_start': False}),
            "AB" : AdaBoostClassifier(**{'learning_rate': 1.8473150336970519, 'n_estimators': 96, 'algorithm': 'SAMME'}), 
            "XGB": XGBClassifier(**{'learning_rate': 0.21528982071549305, 'max_depth': 6, 'n_estimators': 804}),
            "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2','max_iter': 1000}), 
            "SVM" : SVC(**{'C': 1.051871311397777, 'kernel': 'linear', 'max_iter':1000, 'probability': True}), 
            "MLP" : MLPClassifier(**{'activation': 'identity', 'hidden_layer_sizes': 78, 'learning_rate': 'constant','max_iter':500})
88 89 90 91
            }
        # 1.2) Trained with original dataset and cost-sensitive learning
        elif method_id == 1:
            tuned_models = {
Joaquin Torres's avatar
Joaquin Torres committed
92
            "DT": DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'log2', 'criterion': 'entropy', 'class_weight': 'balanced'}),
93 94 95 96 97
            "RF": RandomForestClassifier(**{'criterion': 'entropy', 'max_features': 'sqrt', 'n_estimators': 238, 'class_weight': 'balanced'}),
            "Bagging": BaggingClassifier(**{'max_features': 1.0, 'max_samples': 0.8, 'n_estimators': 22, 'warm_start': False, 'estimator': DecisionTreeClassifier(class_weight='balanced')}),
            "AB": AdaBoostClassifier(**{'learning_rate': 1.7136783954287846, 'n_estimators': 99, 'algorithm': 'SAMME', 'estimator': DecisionTreeClassifier(class_weight='balanced')}),
            "LR": LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000, 'class_weight': 'balanced'}),
            "SVM": SVC(**{'C': 1.480857958217729, 'kernel': 'linear', 'max_iter': 1000, 'class_weight': 'balanced', 'probability': True}),
98 99 100 101
            }
        # 1.3) Trained with oversampled training dataset
        elif method_id == 2:
            tuned_models = {
102 103 104 105 106
            "DT" : DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'sqrt', 'criterion': 'log_loss'}), 
            "RF" : RandomForestClassifier(**{'criterion': 'gini', 'max_features': 'sqrt', 'n_estimators': 121}), 
            "Bagging" : BaggingClassifier(**{'max_features': 1.0, 'max_samples': 1.0, 'n_estimators': 22, 'warm_start': True}),
            "AB" : AdaBoostClassifier(**{'learning_rate': 1.4640913091426446, 'n_estimators': 145, 'algorithm': 'SAMME'}), 
            "XGB": XGBClassifier(**{'learning_rate': 0.19621698151985992, 'max_depth': 7, 'n_estimators': 840}),
107
            "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000}), 
108 109
            "SVM" : SVC(**{'C': 1.590799972846728, 'kernel': 'poly', 'max_iter':1000, 'probability': True}), 
            "MLP" : MLPClassifier(**{'activation': 'relu', 'hidden_layer_sizes': 112, 'learning_rate': 'constant', 'max_iter':500})
110
            }
111
        # 1.4) Trained with undersampled training dataset
112 113
        elif method_id == 3:
            tuned_models = {
114 115 116 117 118 119 120 121
            "DT" : DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'sqrt', 'criterion': 'log_loss'}), 
            "RF" : RandomForestClassifier(**{'criterion': 'gini', 'max_features': 'sqrt', 'n_estimators': 148}), 
            "Bagging" : BaggingClassifier(**{'max_features': 1.0, 'max_samples': 0.8, 'n_estimators': 24, 'warm_start': True}),
            "AB" : AdaBoostClassifier(**{'learning_rate': 1.7970533619575801, 'n_estimators': 122, 'algorithm': 'SAMME'}), 
            "XGB": XGBClassifier(**{'learning_rate': 0.13148624656904934, 'max_depth': 9, 'n_estimators': 723}),
            "LR" : LogisticRegression(**{'solver': 'sag', 'penalty': 'l2', 'max_iter': 1000}), 
            "SVM" : SVC(**{'C': 1.383651513577477, 'kernel': 'poly', 'max_iter':1000, 'probability': True}), 
            "MLP" : MLPClassifier(**{'activation': 'relu', 'hidden_layer_sizes': 89, 'learning_rate': 'invscaling', 'max_iter':500})
122 123 124 125 126 127
            }
    # 2. POST
    else:
        # 2.1) Trained with original dataset
        if method_id == 0:
            tuned_models = {
128 129 130 131 132 133 134 135
            "DT" : DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'sqrt', 'criterion': 'log_loss'}), 
            "RF" : RandomForestClassifier(**{'criterion': 'entropy', 'max_features': 'sqrt', 'n_estimators': 120}), 
            "Bagging" : BaggingClassifier(**{'max_features': 1.0, 'max_samples': 0.8, 'n_estimators': 38, 'warm_start': True}),
            "AB" : AdaBoostClassifier(**{'learning_rate': 1.9069394544838472, 'n_estimators': 121, 'algorithm': 'SAMME'}), 
            "XGB": XGBClassifier(**{'learning_rate': 0.24787889985627387, 'max_depth': 4, 'n_estimators': 956}),
            "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2'}), 
            "SVM" : SVC(**{'C': 1.7965537393241109, 'kernel': 'linear', 'max_iter':1000, 'probability': True}), 
            "MLP" : MLPClassifier(**{'activation': 'relu', 'hidden_layer_sizes': 147, 'learning_rate': 'invscaling', 'max_iter':500})
136 137 138
            }
        # 2.2) Trained with original dataset and cost-sensitive learning
        elif method_id == 1:
139
            tuned_models = {
140 141 142 143 144 145
            "DT": DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'sqrt', 'criterion': 'gini', 'class_weight': 'balanced'}),
            "RF": RandomForestClassifier(**{'criterion': 'entropy', 'max_features': 'sqrt', 'n_estimators': 138, 'class_weight': 'balanced'}),
            "Bagging": BaggingClassifier(**{'max_features': 1.0, 'max_samples': 1.0, 'n_estimators': 66, 'warm_start': True, 'estimator': DecisionTreeClassifier(class_weight='balanced')}),
            "AB": AdaBoostClassifier(**{'learning_rate': 1.92541653518023, 'n_estimators': 114, 'algorithm': 'SAMME', 'estimator': DecisionTreeClassifier(class_weight='balanced')}),
            "LR": LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000, 'class_weight': 'balanced'}),
            "SVM": SVC(**{'C': 0.8395104850983046, 'kernel': 'linear', 'max_iter': 1000, 'class_weight': 'balanced', 'probability': True})
146
            }
147 148
        # 2.3) Trained with oversampled training dataset
        elif method_id == 2:
149
            tuned_models = {
150 151 152 153 154 155 156 157
            "DT" : DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'log2', 'criterion': 'entropy'}), 
            "RF" : RandomForestClassifier(**{'criterion': 'gini', 'max_features': 'sqrt', 'n_estimators': 118}), 
            "Bagging" : BaggingClassifier(**{'max_features': 1.0, 'max_samples': 1.0, 'n_estimators': 56, 'warm_start': False}),
            "AB" : AdaBoostClassifier(**{'learning_rate': 1.5933610622176648, 'n_estimators': 114, 'algorithm': 'SAMME'}), 
            "XGB": XGBClassifier(**{'learning_rate': 0.059934879882855396, 'max_depth': 9, 'n_estimators': 660}),
            "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000}), 
            "SVM" : SVC(**{'C': 1.2237930722499044, 'kernel': 'poly', 'max_iter':1000, 'probability': True}), 
            "MLP" : MLPClassifier(**{'activation': 'identity', 'hidden_layer_sizes': 134, 'learning_rate': 'invscaling', 'max_iter':500})
158
            }
159 160
        # 2.4) Trained with undersampled training dataset
        elif method_id == 3:
161
            tuned_models = {
162 163 164 165 166
            "DT" : DecisionTreeClassifier(**{'splitter': 'best', 'max_features': 'log2', 'criterion': 'log_loss'}), 
            "RF" : RandomForestClassifier(**{'criterion': 'gini', 'max_features': 'sqrt', 'n_estimators': 151}), 
            "Bagging" : BaggingClassifier(**{'max_features': 1.0, 'max_samples': 1.0, 'n_estimators': 20, 'warm_start': False}),
            "AB" : AdaBoostClassifier(**{'learning_rate': 1.6523810056317618, 'n_estimators': 89, 'algorithm': 'SAMME'}), 
            "XGB": XGBClassifier(**{'learning_rate': 0.18430397856234193, 'max_depth': 4, 'n_estimators': 956}),
167
            "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000}), 
168 169
            "SVM" : SVC(**{'C': 1.1807459108651588, 'kernel': 'linear', 'max_iter':1000, 'probability': True}), 
            "MLP" : MLPClassifier(**{'activation': 'identity', 'hidden_layer_sizes': 55, 'learning_rate': 'constant', 'max_iter':500})
170
            }
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    return tuned_models
# --------------------------------------------------------------------------------------------------------

# Scorers
# --------------------------------------------------------------------------------------------------------
def TN_scorer(clf, X, y):
    """Gives the number of samples predicted as true negatives"""
    y_pred = clf.predict(X)
    cm = confusion_matrix(y, y_pred)
    TN = cm[0,0]
    return TN
def FN_scorer(clf, X, y):
    """Gives the number of samples predicted as false negatives"""
    y_pred = clf.predict(X)
    cm = confusion_matrix(y, y_pred)
    FN = cm[0,1]
    return FN
def FP_scorer(clf, X, y):
    """Gives the number of samples predicted as false positive"""
    y_pred = clf.predict(X)
    cm = confusion_matrix(y, y_pred)
    FP = cm[1,0]
    return FP
def TP_scorer(clf, X, y):
    """Gives the number of samples predicted as true positive"""
    y_pred = clf.predict(X)
    cm = confusion_matrix(y, y_pred)
    TP = cm[1,1]
    return TP

def negative_recall_scorer(clf, X, y):
    """Gives the negative recall defined as the (number of true_negative_samples)/(total number of negative samples)"""
    y_pred = clf.predict(X)
    cm = confusion_matrix(y, y_pred)
    TN_prop = cm[0,0]/(cm[0,1]+cm[0,0])
    return TN_prop
# --------------------------------------------------------------------------------------------------------

if __name__ == "__main__":
    # Reading testing data
    data_dic = read_test_data()

    # Setup
    # --------------------------------------------------------------------------------------------------------
    # Scorings to use for model evaluation
    scorings = {
        'F1':make_scorer(f1_score), 
        'NREC': negative_recall_scorer, 
        'REC':make_scorer(recall_score), 
        'PREC':make_scorer(precision_score), 
        'ACC': make_scorer(accuracy_score),
        'TN':TN_scorer, 
        'FN':FN_scorer, 
        'FP':FP_scorer, 
225
        'TP':TP_scorer,
Joaquin Torres's avatar
Joaquin Torres committed
226 227
        'AUROC': make_scorer(roc_auc_score),  # AUROC requires decision function or probability outputs
        'AUPRC': make_scorer(average_precision_score)  # AUPRC requires probability outputs
228
        } 
229 230 231 232 233 234 235 236 237 238 239 240 241
    method_names = {
        0: "ORIG",
        1: "ORIG_CW",
        2: "OVER",
        3: "UNDER"
    }
    # --------------------------------------------------------------------------------------------------------

    # Evaluating performance using test dataset
    # --------------------------------------------------------------------------------------------------------
    scores_sheets = {} # To store score dfs as sheets in the same excel file
    for i, group in enumerate(['pre', 'post']):
        # Get test dataset based on group
Joaquin Torres's avatar
Joaquin Torres committed
242 243
        X_test = data_dic['X_test_' + group]
        y_test = data_dic['y_test_' + group]
244
        for j, method in enumerate(['', '', 'over_', 'under_']):
Joaquin Torres's avatar
Joaquin Torres committed
245
            print(f"{group}-{method}")
Joaquin Torres's avatar
Joaquin Torres committed
246 247 248
            # Get train dataset based on group and method
            X_train = data_dic['X_train_' + method + group]
            y_train = data_dic['y_train_' + method + group]
249 250 251 252
            # Get tuned models for this group and method
            models = get_tuned_models(group_id=i, method_id=j)
            # Scores df
            scores_df = pd.DataFrame(index=models.keys(), columns=scorings.keys())
253
            # Create a figure for all models in this group-method
254
            fig, axes = plt.subplots(len(models), 3, figsize=(10, 8 * len(models)))
255 256
            if len(models) == 1:  # Adjustment if there's only one model (axes indexing issue)
                axes = [axes]
257
            # Evaluate each model
258
            for model_idx, (model_name, model) in enumerate(models.items()):
Joaquin Torres's avatar
Joaquin Torres committed
259
                # ----------- TEMPORAL -------------
Joaquin Torres's avatar
Joaquin Torres committed
260 261 262 263 264 265 266 267 268 269 270 271
                # Train the model (it was just initialized above)
                model.fit(X_train, y_train)
                if hasattr(model, "decision_function"):
                    y_score = model.decision_function(X_test)
                else:
                    y_score = model.predict_proba(X_test)[:, 1]  # Use probability of positive class
                # Calculate ROC curve and ROC area for each class
                fpr, tpr, _ = roc_curve(y_test, y_score, pos_label=model.classes_[1])
                roc_display = RocCurveDisplay(fpr=fpr, tpr=tpr).plot(ax=axes[model_idx][0]) 
                # Calculate precision-recall curve
                precision, recall, _ = precision_recall_curve(y_test, y_score, pos_label=model.classes_[1])
                pr_display = PrecisionRecallDisplay(precision=precision, recall=recall).plot(ax=axes[model_idx][1])
272 273 274
                # Get confusion matrix plot
                y_pred = model.predict(X_test)
                cm = confusion_matrix(y_test, y_pred)
Joaquin Torres's avatar
Joaquin Torres committed
275
                ConfusionMatrixDisplay(cm).plot(ax=axes[model_idx][2])
276
                # Give name to plots
Joaquin Torres's avatar
Joaquin Torres committed
277 278
                axes[model_idx][0].set_title(f'ROC Curve for {model_name}')
                axes[model_idx][1].set_title(f'PR Curve for {model_name}')
279
                axes[model_idx][2].set_title(f'CM for {model_name}')
Joaquin Torres's avatar
Joaquin Torres committed
280 281 282 283
                # Evaluate at each of the scores of interest
                for score_name, scorer in scorings.items():
                    score_value = scorer(model, X_test, y_test)
                    scores_df.at[model_name, score_name] = score_value
284 285
            # Adjust layout and save/show figure
            plt.tight_layout()
286
            plt.savefig(f'./test_results/aux_plots/{group}_{method_names[j]}.svg', format='svg', dpi=500)
287
            plt.close(fig)
288 289 290 291
            # Store the DataFrame in the dictionary with a unique key for each sheet
            sheet_name = f"{group}_{method_names[j]}"
            scores_sheets[sheet_name] = scores_df
    # Write results to Excel file
Joaquin Torres's avatar
Joaquin Torres committed
292
    with pd.ExcelWriter('./test_results/testing_tuned_models.xlsx') as writer:
293 294 295 296 297
        for sheet_name, data in scores_sheets.items():
            data.to_excel(writer, sheet_name=sheet_name)
    # --------------------------------------------------------------------------------------------------------