【问题标题】:Creating a scoring metric for IsolationForest GridSearchCV为 IsolationForest GridSearchCV 创建评分指标
【发布时间】:2018-04-19 13:09:52
【问题描述】:

我正在做一个 IsolationForest,我想使用 GridSearchCV 优化我的超参数。我希望我的评分基于异常值的召回分数,即标签 = -1。但是,我在运行此代码时遇到了错误。

recall_fraud = make_scorer(recall_score(pos_label=-1))
gs_params ={
        'max_samples': [300,500,1000],
        'contamination': [float(y_train.count(-1))/len(y_train)] ,
        'max_features': [1,3,7],
        'n_estimators':[1000],
        'random_state':[1]
    }

isof_gs = GridSearchCV(IsolationForest(), gs_params, n_jobs = 1, verbose = 1, cv = 5, scoring = recall_fraud) 

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-273-d1f260f73f29> in <module>()
----> 1 recall_fraud = make_scorer(recall_score(pos_label=-1))
      2 gs_params ={
      3     'max_samples': [300,500,1000],
      4     'contamination': [float(y_train.count(-1))/len(y_train)] ,
      5     'max_features': [1,3,7],

TypeError: recall_score() takes at least 2 arguments (1 given)

我做错了什么吗?

【问题讨论】:

    标签: python python-2.7 scikit-learn


    【解决方案1】:

    使用make_scorer 时,您要传递给评分函数的所有关键字参数都应传递给make_scorer,而不是内部评分函数。

    kwargs param in make_scorer:-

    **kwargs : 附加参数 要传递给 score_func 的附加参数。

    像这样更改您的代码:

    # Updated recall_score() to recall_score
    recall_fraud = make_scorer(recall_score, pos_label=-1)
    

    它不会再抛出错误了。

    pos_label在实际计算grid-search的recall时会自动转发到recall_score方法。

    【讨论】:

    • 嗯。即使我按照您的方法,仍然会出现相同的错误。对你有用吗?
    • @TimOng 啊,是的,我的错。我需要从recall_score 中删除括号。更新了答案以反映相同。
    猜你喜欢
    • 2011-09-18
    • 2018-11-05
    • 2021-07-11
    • 2020-03-07
    • 2019-01-13
    • 2018-10-22
    • 2019-05-27
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多