'How is AUC, Precision, Recall being calculated in this loop
This code block creates a dictionary of models and iterates through scoring and models creating three key value pairs.
The format identified by {0},{1} is mapped by the eval() method using j (the score values) and the model_name strings which are matched by index to the model names.
{0} is replaced by j and {1} is replaced by model_name[model.index(i)].
Only the variables inside the string i.e. {0} and {1} are evaluated and extraneous characters that aren't a variable or operator i.e. Mean_ and _CV is ignored. So how then are the actual AUC, Precision and Recall values calculated?
model = ['Logistic Regression','KNN','Gaussian NB','Decision Trees','Random Forest','Ensemble']
scoring = ['AUC','Precision','Recall']
model_name = ['Logit','KNN','NB','tree','forest','ensemble']
model_list = []
for i in model:
for j in scoring:
model_dic = {'Model': i,'Scoring':j, 'Score':eval('Mean_{0}_{1}_CV'.format(j,model_name[model.index(i)]))}
model_list.append(model_dic)
Output
# Model | Scoring | Score
# Logistic Regression AUC 0.957516
# ...
Solution 1:[1]
model_dic = {'Model': i,'Scoring':j, 'Score':eval('Mean_{0}_{1}_CV'.format(j,model_name[model.index(i)]))}
This line does too much all at once. To understand what it is doing, break it into smaller pieces:
formatted = 'Mean_{0}_{1}_CV'.format(j,model_name[model.index(i)])
evaluated = eval(formatted)
model_dic = {'Model': i,'Scoring':j, 'Score':evaluated}
Now you can add print() statements to understand better what is happening:
formatted = 'Mean_{0}_{1}_CV'.format(j,model_name[model.index(i)])
print(formatted)
evaluated = eval(formatted)
print(evaluated)
model_dic = {'Model': i,'Scoring':j, 'Score':evaluated}
print(model_dic)
I suggest that you read about the format() and eval() functions to understand the output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Code-Apprentice |
