create different list variable names using a for loop

I am looking to use cross validation for 3 different ML classifiers and would like to get all results without repetitive code

model_01 = RandomForestClassifier(max_depth = 2, random_state = 0)
model_02 = XGBClassifier(max_depth = 2, n_estimators=200)
model_02 = LogisticRegression()
classifier= [model_01,model_02,model_03] 

for i in classifier: 
  kf = StratifiedKFold(n_splits=5, random_state=random_seed)
  cross_val_f1_score_lst = []
  cross_val_accuracy_lst = []
  cross_val_recall_lst = []
  cross_val_precision_lst = []

I wont go through the ML code since it is not necessary for this problem. But I would like to append 5 entries for each list given above. With all the entries suggested above, I would want separate set of lists for all three different classifiers mentioned above (with list names such as cross_val_f1_score_lst_model_01/02/03 etc). The rest of the relevant code for one classifier iteration is given below.

  #mean values for all metrics
  cross_val_recall_lst.append(recall_score(target_val, validation_preds))
  cross_val_accuracy_lst.append(accuracy_score(target_val, validation_preds))
  cross_val_precision_lst.append(precision_score(target_val, validation_preds))
  cross_val_f1_score_lst.append(f1_score(target_val, validation_preds))

  print('Cross validated overall financial loss: {}'.format(np.mean(cross_val_financial_loss_lst)))
  print('Cross validated accuracy: {}'.format(np.mean(cross_val_accuracy_lst)))
  print('Cross validated recall score: {}'.format(np.mean(cross_val_recall_lst)))
  print('Cross validated precision score: {}'.format(np.mean(cross_val_precision_lst)))
  print('Cross validated f1_score: {}'.format(np.mean(cross_val_f1_score_lst)))

Any solution will be highly appreciated. Feel free if I am using any incorrect terminology.



Read more here: https://stackoverflow.com/questions/67153095/create-different-list-variable-names-using-a-for-loop

Content Attribution

This content was originally published by sv176 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: