added loopity_loop to run multiple models with stratified k-fold, got stuck in infinite loops and nested dicts

This commit is contained in:
Tanushree Tunstall 2022-03-14 10:36:19 +00:00
parent 69d0c1b557
commit 7aead2d4f4
18 changed files with 287 additions and 62 deletions

View file

@ -97,13 +97,13 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
,'Precision': []
,'Recall': []
,'Accuracy': []
,'ROC_curve': []
#,'ROC_AUC': []
}
models = [
('Logistic Regression' , log_reg)
#, ('Naive Bayes' , nb)
, ('Naive Bayes' , nb)
, ('K-Nearest Neighbors', knn)
# , ('SVM' , svm)
, ('SVM' , svm)
# , ('MLP' , mlp)
# , ('Decision Tree' , dt)
# , ('Extra Trees' , et)
@ -132,10 +132,8 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
y_train_fold, y_test_fold = y_targetF.iloc[train_index], y_targetF.iloc[test_index]
#print("Fold: ", fold_no, len(train_index), len(test_index))
# for keys in skf_dict:
for model_name, model in models:
print("start of model", model_name, " loop", fold_no)
print("\nStart of model", model_name, "\nLoop no.", fold_no)
#skf_dict.update({model_name: classification_metrics })
model_pipeline = Pipeline(steps=[('prep' , col_transform)
, ('classifier' , model)])
@ -145,28 +143,39 @@ def MultClassPipeSKF(input_df, y_targetF, var_type = ['numerical', 'categorical'
#----------------
# Model metrics
#----------------
score=f1_score(y_test_fold, y_pred_fold)
mcc = matthews_corrcoef(y_test_fold, y_pred_fold)
fscore = f1_score(y_test_fold, y_pred_fold)
mcc = matthews_corrcoef(y_test_fold, y_pred_fold)
#pres = precision_score(y_test_fold, y_pred_fold)
#recall = recall_score(y_test_fold, y_pred_fold)
pres = precision_score(y_test_fold, y_pred_fold, zero_division=0)
recall = recall_score(y_test_fold, y_pred_fold, zero_division=0)
accu = accuracy_score(y_test_fold, y_pred_fold)
#roc_auc = roc_auc_score(y_test_fold, y_pred_fold)
fold=("fold_"+str(fold_no))
fold_dict[model_name].update({fold: {}})
pp.pprint(fold_dict)
print("end of model", model_name, " loop", fold_no)
#pp.pprint(fold_dict)
print("\nEnd of model", model_name, "\nLoop no.", fold_no)
fold_dict[model_name][fold].update(classification_metrics)
#fold_dict[model_name][fold]['F1_score'].append(score)
fold_dict[model_name][fold].update({'F1_score': score})
fold_dict[model_name][fold].update({'MCC': mcc})
fold_dict[model_name][fold].update({'F1_score' : fscore})
fold_dict[model_name][fold].update({'MCC' : mcc})
fold_dict[model_name][fold].update({'Precision' : pres})
fold_dict[model_name][fold].update({'Recall' : recall})
fold_dict[model_name][fold].update({'Accuracy' : accu})
#fold_dict[model_name][fold].update({'ROC_AUC' : roc_auc})
fold_no +=1
#pp.pprint(skf_dict)
return(fold_dict)
t3_res = MultClassPipeSKF(input_df = numerical_features_df
, y_targetF = target1
, var_type = 'numerical'
, skf_splits = 10)
#pp.pprint(t3_res)
#print(t3_res)
#%% CAll function
# t3_res = MultClassPipeSKF(input_df = numerical_features_df
# , y_targetF = target1
# , var_type = 'numerical'
# , skf_splits = 10)
# pp.pprint(t3_res)
# #print(t3_res)