在这个循环中如何计算 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被忽略。那么实际的AUCPrecisionRecall值是如何计算的?

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
# ...

原文链接:https://stackoverflow.com//questions/71463514/how-is-auc-precision-recall-being-calculated-in-this-loop

回复

我来回复
  • Code-Apprentice的头像
    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年前 0条评论