当前位置:   article > 正文

GridSearchCV 、cross_val_score中scoring(模型评价标准)的自定义方法_rfecv自定义score的函数

rfecv自定义score的函数

区别于xgb.cv模型评估函数的自定义方法链接
1、xgb.cv中【metrics的自定义】,需要注意str返回值与函数名保持一致
2、GridSearchCV 中 【scoring的自定义】,需要注意函数定义后,使用make_scorer封装

在这里插入图片描述

1、自定义模型评估函数
# 自定义函数
def custom_eval(y_true, y_pred):
    ''' 
    func:
        自定义模型评估函数:
                GridSearchCV(scoring=custom_eval)
    para:
        y_true: 0/1_label
        y_pred: 预测结果
    return:
    	float
    '''
    
    precision,recall,threshold=metrics.precision_recall_curve(y_true,y_pred,pos_label=0)
    pr=pd.DataFrame({'precision':precision,'recall':recall})
    ret = pr[pr.precision>=0.5].recall.max()
    return ret
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

使用make_scorer,构建完全自定义 score

from sklearn.metrics import  make_scorer
score = make_scorer(custom_eval)
  • 1
  • 2

自定义scorer使用

param_test1 = {
    'max_depth':range(3,10,1),
    'min_child_weight':range(4,8,1)
    }

gsearch1 = GridSearchCV(
    estimator = clf2,           # 选择使用的模型
    param_grid = param_test1,   # 需要最优化的参数的取值,值为字典或者列表
    scoring=score,           # 模型评价标准
    n_jobs=4,                   # n_jobs: 并行数。-1:跟CPU核数一致, 1:默认值
    cv=5,                       # 交叉验证参数,默认None,使用三折交叉验证
    verbose=0,                  # 日志冗长度,int:冗长度,0:不输出训练过程,# 1:偶尔输出,>1:对每个子模型都输出。
    )

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
2 使用系统预定义模型评估函数

在这里插入图片描述

2.1 系统预定义模型评估函数如下:

在这里插入图片描述
在这里插入图片描述
参考文献:链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/287886
推荐阅读
相关标签
  

闽ICP备14008679号