【问题标题】:Evaluation of multiple models using GridSearchCV and f2 score使用 GridSearchCV 和 f2 分数评估多个模型
【发布时间】:2021-07-12 19:13:18
【问题描述】:

我正在尝试使用 GridSearchCV 在二进制分类中对一些机器学习模型进行分类。我想根据分数、最佳参数和 f2 分数对模型进行分类。

对于分数和最佳参数我使用了代码

scores = []

for model_name, mp in model_params.items():
    clf =  GridSearchCV(mp['model'], mp['params'], cv=5, return_train_score=False)
    clf.fit(X_test, y_test)
    scores.append({
        'model': model_name,
        'best_score': clf.best_score_,
        'best_params': clf.best_params_
    })
    
df = pd.DataFrame(scores,columns=['model','best_score','best_params'])

它为所有模型提供了best_scorebest_params,但我无法找到所有模型的 f2 分数。代码应该是什么?

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:
    from sklearn.metrics import make_scorer,fbeta_score
    def f2_func(y_true, y_pred):
        f2_score = fbeta_score(y_true, y_pred, beta=2)
        return f2_score
    
    def my_f2_scorer():
        return make_scorer(f2_func)
    
    clf = GridSearchCV(svm.SVC(), parameters, cv=10, scoring=my_f2_scorer())
    

    【讨论】:

    • 请为答案添加详细说明,以进一步阐明您的代码如何解决所要求的问题。 span>
    【解决方案2】:

    您可能想查看clf.cv_results_,它包含所有搜索模型的分数和其他基准信息(另请参阅here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2020-11-06
      • 2015-10-28
      • 2020-10-09
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多