python xgboost实战_使用python日志记录将XGBoost的xgb.train的输出保存为日志文件

fromloggingimportgetLogger,basicConfig,INFOimportnumpyasnpimportxgboostasxgbfromsklearn.datasetsimportload_digitsfromsklearn.model_selectionimportKFold# Some logging settingsbasicConfig(level=INFO)logger=getLogger(__name__)deflog_evaluation(period=1,show_stdv=True):"""Create a callback that logs evaluation result with logger.

Parameters

----------

period : int

The period to log the evaluation results

show_stdv : bool, optional

Whether show stdv if provided

Returns

-------

callback : function

A callback that logs evaluation every period iterations into logger.

"""def_fmt_metric(value,show_stdv=True):"""format metric string"""iflen(value)==2:return'%s:%g'%(value[0],value[1])eliflen(value)==3:ifshow_stdv:return'%s:%g+%g'%(value[0],value[1],value[2])else:return'%s:%g'%(value[0],value[1])else:raiseValueError("wrong metric value")defcallback(env):ifenv.rank!=0orlen(env.evaluation_result_list)==0orperiodisFalse:returni=env.iterationifi%period==0ori+1==env.begin_iterationori+1==env.end_iteration:msg='\t'.join([_fmt_metric(x,show_stdv)forxinenv.evaluation_result_list])logger.info('[%d]\t%s\n'%(i,msg))returncallback

rng=np.random.RandomState(31337)print("Zeros and Ones from the Digits dataset: binary classification")digits=load_digits(2)y=digits['target']X=digits['data']kf=KFold(n_splits=2,shuffle=True,random_state=rng)fortrain_index,test_indexinkf.split(X):param={'max_depth':2,'eta':0.3,'silent':1,'objective':'binary:logistic'}dtrain=xgb.DMatrix(X[train_index],y[train_index])dtest=xgb.DMatrix(X[test_index],y[test_index])# specify validations set to watch performancewatchlist=[(dtest,'eval'),(dtrain,'train')]num_round=2# add loggercallbacks=[log_evaluation(1,True)]bst=xgb.train(param,dtrain,num_round,watchlist,callbacks=callbacks)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值