在这个循环中如何计算 AUC、Precision、Recall
python 203
原文标题 :How is AUC, Precision, Recall being calculated in this loop
此代码块创建一个模型字典,并通过评分和模型迭代创建三个键值对。
{0},{1}
标识的格式由eval()
方法使用j
(分数值)和index
匹配的model_name
字符串映射到model
名称。
{0}
被j
替换,{1}
被model_name[model.index(i)]
替换。
只有字符串中的变量,即{0}
和{1}
被评估,并且不是变量或运算符的无关字符,即Mean_
和_CV
被忽略。那么实际的AUC
,Precision
和Recall
值是如何计算的?
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)
输出
# Model | Scoring | Score
# Logistic Regression AUC 0.957516
# ...
回复
我来回复-
Code-Apprentice 评论
该回答已被采纳!
model_dic = {'Model': i,'Scoring':j, 'Score':eval('Mean_{0}_{1}_CV'.format(j,model_name[model.index(i)]))}
这条线一次做的太多了。要了解它在做什么,请将其分解为更小的部分:
formatted = 'Mean_{0}_{1}_CV'.format(j,model_name[model.index(i)]) evaluated = eval(formatted) model_dic = {'Model': i,'Scoring':j, 'Score':evaluated}
现在您可以添加
print()
语句以更好地了解正在发生的事情: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)
我建议您阅读
format()
和eval()
函数以了解输出。2年前