赞
踩
区别于xgb.cv模型评估函数的自定义方法链接
1、xgb.cv中【metrics的自定义】,需要注意str返回值与函数名保持一致
2、GridSearchCV 中 【scoring的自定义】,需要注意函数定义后,使用make_scorer封装
# 自定义函数
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
使用make_scorer,构建完全自定义 score
from sklearn.metrics import make_scorer
score = make_scorer(custom_eval)
自定义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:对每个子模型都输出。
)
参考文献:链接
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。