From d3a20982cc4b53189920d765725fa97fd067cccd Mon Sep 17 00:00:00 2001 From: joaquintb Date: Fri, 10 May 2024 13:37:13 +0200 Subject: [PATCH] identified problem with svm: need probability=true for AUROC --- model_selection/test_models.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/model_selection/test_models.py b/model_selection/test_models.py index 422843b..58c433c 100644 --- a/model_selection/test_models.py +++ b/model_selection/test_models.py @@ -8,7 +8,7 @@ import pandas as pd import numpy as np from xgboost import XGBClassifier from sklearn.metrics import confusion_matrix -from sklearn.metrics import f1_score, make_scorer, precision_score, recall_score, accuracy_score +from sklearn.metrics import f1_score, make_scorer, precision_score, recall_score, accuracy_score, roc_auc_score, average_precision_score from sklearn.ensemble import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier from sklearn.neural_network import MLPClassifier from sklearn.svm import SVC @@ -49,7 +49,7 @@ def get_tuned_models(group_id, method_id): "AB" : AdaBoostClassifier(**{'learning_rate': 1.9189147333140566, 'n_estimators': 131, 'algorithm': 'SAMME'}), "XGB": XGBClassifier(**{'learning_rate': 0.22870029177880222, 'max_depth': 8, 'n_estimators': 909}), "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': None, 'max_iter': 1000}), - "SVM" : SVC(**{'C': 0.9872682949695772, 'kernel': 'linear', 'max_iter':1000}), + #"SVM" : SVC(**{'C': 0.9872682949695772, 'kernel': 'linear', 'max_iter':1000}), "MLP" : MLPClassifier(**{'activation': 'identity', 'hidden_layer_sizes': 122, 'learning_rate': 'invscaling', 'max_iter':500}) } # 1.2) Trained with original dataset and cost-sensitive learning @@ -60,7 +60,7 @@ def get_tuned_models(group_id, method_id): "Bagging": BaggingClassifier(**{'max_features': 1.0, 'max_samples': 1.0, 'n_estimators': 15, 'warm_start': False, 'estimator': DecisionTreeClassifier(class_weight='balanced')}), "AB": AdaBoostClassifier(**{'learning_rate': 0.8159074545140872, 'n_estimators': 121, 'algorithm': 'SAMME', 'estimator': DecisionTreeClassifier(class_weight='balanced')}), "LR": LogisticRegression(**{'solver': 'lbfgs', 'penalty': None, 'max_iter': 1000, 'class_weight': 'balanced'}), - "SVM": SVC(**{'C': 1.5550524351360953, 'kernel': 'linear', 'max_iter': 1000, 'class_weight': 'balanced'}), + #"SVM": SVC(**{'C': 1.5550524351360953, 'kernel': 'linear', 'max_iter': 1000, 'class_weight': 'balanced'}), } # 1.3) Trained with oversampled training dataset elif method_id == 2: @@ -71,7 +71,7 @@ def get_tuned_models(group_id, method_id): "AB" : AdaBoostClassifier(**{'learning_rate': 1.6590924545876917, 'n_estimators': 141, 'algorithm': 'SAMME'}), "XGB": XGBClassifier(**{'learning_rate': 0.26946295284728783, 'max_depth': 7, 'n_estimators': 893}), "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000}), - "SVM" : SVC(**{'C': 1.676419306008229, 'kernel': 'poly', 'max_iter':1000}), + #"SVM" : SVC(**{'C': 1.676419306008229, 'kernel': 'poly', 'max_iter':1000}), "MLP" : MLPClassifier(**{'activation': 'relu', 'hidden_layer_sizes': 116, 'learning_rate': 'invscaling', 'max_iter':500}) } # 1.4) Trained with undersampled training dataset @@ -83,7 +83,7 @@ def get_tuned_models(group_id, method_id): "AB" : AdaBoostClassifier(**{'learning_rate': 1.6996764264041269, 'n_estimators': 93, 'algorithm': 'SAMME'}), "XGB": XGBClassifier(**{'learning_rate': 0.26480707899668926, 'max_depth': 7, 'n_estimators': 959}), "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': None, 'max_iter': 1000}), - "SVM" : SVC(**{'C': 1.1996501173654208, 'kernel': 'poly', 'max_iter':1000}), + #"SVM" : SVC(**{'C': 1.1996501173654208, 'kernel': 'poly', 'max_iter':1000}), "MLP" : MLPClassifier(**{'activation': 'relu', 'hidden_layer_sizes': 131, 'learning_rate': 'constant', 'max_iter':500}) } # 2. POST @@ -97,7 +97,7 @@ def get_tuned_models(group_id, method_id): "AB" : AdaBoostClassifier(**{'learning_rate': 1.7806904141367559, 'n_estimators': 66, 'algorithm': 'SAMME'}), "XGB": XGBClassifier(**{'learning_rate': 0.21889089898592098, 'max_depth': 6, 'n_estimators': 856}), "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': None, 'max_iter': 1000}), - "SVM" : SVC(**{'C': 1.9890638540240584, 'kernel': 'linear', 'max_iter':1000}), + #"SVM" : SVC(**{'C': 1.9890638540240584, 'kernel': 'linear', 'max_iter':1000}), "MLP" : MLPClassifier(**{'activation': 'logistic', 'hidden_layer_sizes': 112, 'learning_rate': 'constant', 'max_iter':500}) } # 2.2) Trained with original dataset and cost-sensitive learning @@ -108,7 +108,7 @@ def get_tuned_models(group_id, method_id): "Bagging": BaggingClassifier(**{'max_features': 1.0, 'max_samples': 0.8, 'n_estimators': 11, 'warm_start': True, 'estimator': DecisionTreeClassifier(class_weight='balanced')}), "AB": AdaBoostClassifier(**{'learning_rate': 1.7102248217141944, 'n_estimators': 108, 'algorithm': 'SAMME', 'estimator': DecisionTreeClassifier(class_weight='balanced')}), "LR": LogisticRegression(**{'solver': 'lbfgs', 'penalty': None, 'max_iter': 1000, 'class_weight': 'balanced'}), - "SVM": SVC(**{'C': 1.1313840454519628, 'kernel': 'sigmoid', 'max_iter': 1000, 'class_weight': 'balanced'}) + #"SVM": SVC(**{'C': 1.1313840454519628, 'kernel': 'sigmoid', 'max_iter': 1000, 'class_weight': 'balanced'}) } # 2.3) Trained with oversampled training dataset elif method_id == 2: @@ -131,7 +131,7 @@ def get_tuned_models(group_id, method_id): "AB" : AdaBoostClassifier(**{'learning_rate': 1.836659462701278, 'n_estimators': 138, 'algorithm': 'SAMME'}), "XGB": XGBClassifier(**{'learning_rate': 0.2517946893282251, 'max_depth': 4, 'n_estimators': 646}), "LR" : LogisticRegression(**{'solver': 'lbfgs', 'penalty': 'l2', 'max_iter': 1000}), - "SVM" : SVC(**{'C': 1.8414678085000697, 'kernel': 'linear', 'max_iter':1000}), + #"SVM" : SVC(**{'C': 1.8414678085000697, 'kernel': 'linear', 'max_iter':1000}), "MLP" : MLPClassifier(**{'activation': 'relu', 'hidden_layer_sizes': 76, 'learning_rate': 'constant', 'max_iter':500}) } return tuned_models @@ -188,9 +188,10 @@ if __name__ == "__main__": 'TN':TN_scorer, 'FN':FN_scorer, 'FP':FP_scorer, - 'TP':TP_scorer + 'TP':TP_scorer, + 'AUROC': make_scorer(roc_auc_score, needs_threshold=True), # AUROC requires decision function or probability outputs + 'AUPRC': make_scorer(average_precision_score, needs_proba=True) # AUPRC requires probability outputs } - # AUROC and AUPRC (plot?) method_names = { 0: "ORIG", 1: "ORIG_CW", -- 2.24.1