This commit is contained in:
Tanushree Tunstall 2022-03-10 19:20:02 +00:00
parent d733b980ba
commit 69d0c1b557
5 changed files with 607 additions and 31 deletions

View file

@ -92,15 +92,17 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
clfs = [
('Logistic Regression' , log_reg)
, ('Naive Bayes' , nb)
#, ('Naive Bayes' , nb)
, ('K-Nearest Neighbors', knn)
, ('SVM' , svm)
, ('MLP' , mlp)
, ('Decision Tree' , dt)
, ('Extra Trees' , et)
, ('Random Forest' , rf)
, ('Random Forest2' , rf2)
, ('XGBoost' , xgb)
, ('Naive Bayes' , nb)
#, ('Random Forest2' , rf2)
#, ('XGBoost' , xgb)
]
skf = StratifiedKFold(n_splits = skf_splits
@ -112,17 +114,20 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
Y = y_targetF
# Initialise score metrics list to store skf results
fscoreL = []
mccL = []
presL = []
recallL = []
accuL = []
roc_aucL = []
# fscoreL = []
# mccL = []
# presL = []
# recallL = []
# accuL = []
# roc_aucL = []
skf_dict = {}
#scores_df = pd.DataFrame()
for train_index, test_index in skf.split(input_df, y_targetF):
x_train_fold, x_test_fold = input_df.iloc[train_index], input_df.iloc[test_index]
y_train_fold, y_test_fold = y_targetF.iloc[train_index], y_targetF.iloc[test_index]
#fscoreL = {}
# for train_index, test_index in skf.split(X_array, Y):
# print('\nSKF train index:', train_index
# , '\nSKF test index:', test_index)
@ -139,7 +144,7 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
, ('classifier' , clf)])
# model_pipeline = Pipeline(steps=[('prep' , MinMaxScaler())
# , ('classifier' , clf)])
# , ('classifier' , clf)])
model_pipeline.fit(x_train_fold, y_train_fold)
@ -150,33 +155,34 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
#----------------
# F1-Score
fscore = f1_score(y_test_fold, y_pred_fold)
fscoreL.append(fscore)
fscoreM = mean(fscoreL)
fscoreL[clf_name].append(fscore)
print('fscoreL Len: ', len(fscoreL))
#fscoreM = mean(fscoreL[clf])
# Matthews correlation coefficient
mcc = matthews_corrcoef(y_test_fold, y_pred_fold)
mccL.append(mcc)
mccL[clf_name].append(mcc)
mccM = mean(mccL)
# Precision
pres = precision_score(y_test_fold, y_pred_fold)
presL.append(pres)
presM = mean(presL)
# # Precision
# pres = precision_score(y_test_fold, y_pred_fold)
# presL.append(pres)
# presM = mean(presL)
# Recall
recall = recall_score(y_test_fold, y_pred_fold)
recallL.append(recall)
recallM = mean(recallL)
# # Recall
# recall = recall_score(y_test_fold, y_pred_fold)
# recallL.append(recall)
# recallM = mean(recallL)
# Accuracy
accu = accuracy_score(y_test_fold, y_pred_fold)
accuL.append(accu)
accuM = mean(accuL)
# # Accuracy
# accu = accuracy_score(y_test_fold, y_pred_fold)
# accuL.append(accu)
# accuM = mean(accuL)
# ROC_AUC
roc_auc = roc_auc_score(y_test_fold, y_pred_fold)
roc_aucL.append(roc_auc)
roc_aucM = mean(roc_aucL)
# # ROC_AUC
# roc_auc = roc_auc_score(y_test_fold, y_pred_fold)
# roc_aucL.append(roc_auc)
# roc_aucM = mean(roc_aucL)
clf_scores_df = clf_scores_df.append({'Model' : clf_name
,'F1_score' : fscoreM
@ -186,4 +192,6 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
, 'Accuracy' : accuM
, 'ROC_curve': roc_aucM}
, ignore_index = True)
return clf_scores_df
return(clf_scores_df)
#scores_df = scores_df.append(clf_scores_df)
# return clf_scores_df