Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python日志记录将XGBoost的xgb.train的输出保存为日志文件_Python_Logging_Xgboost - Fatal编程技术网

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

使用python日志记录将XGBoost的xgb.train的输出保存为日志文件,python,logging,xgboost,Python,Logging,Xgboost,我试图通过logging将XGBoost的xgb.train的输出保存为日志文件,但无法记录输出。我怎样才能录下来?我试图提及现有的Stackoverflow问题,但这是不可能的。我想请你拿一个具体的样品给我看看 导入系统 导入日志记录 # ---------------------------------------------- # #一些日志设置 # ---------------------------------------------- # 将xgboost作为xgb导入 将nump

我试图通过
logging
将XGBoost的
xgb.train
的输出保存为日志文件,但无法记录输出。我怎样才能录下来?我试图提及现有的Stackoverflow问题,但这是不可能的。我想请你拿一个具体的样品给我看看

导入系统 导入日志记录 # ---------------------------------------------- # #一些日志设置 # ---------------------------------------------- # 将xgboost作为xgb导入 将numpy作为np导入 从sklearn.model_选择导入KFold 从sklearn.dataset导入load_数字 rng=np.random.RandomState(31337) 打印(“数字数据集中的零和一:二进制分类”) 位数=加载\位数(2) y=数字['target'] X=数字[“数据”] kf=KFold(n_splits=2,shuffle=True,random_state=rng) 对于列车索引,测试kf中的列车索引。拆分(X): param={'max_depth':2,'eta':0.3,'silent':1,'objective':'binary:logistic'} dtrain=xgb.DMatrix(X[列索引],y[列索引]) dtest=xgb.DMatrix(X[测试指数],y[测试指数]) #指定设置为监视性能的验证 观察列表=[(数据测试,'eval'),(数据训练,'train')] 轮数=2 bst=xgb.列车(参数、数据列车、轮数、值班表) #我想记录这个输出。 #数字数据集中的0和1:二进制分类 #[0]评估错误:0.011111列车错误:0.011111 #[1]评估错误:0.011111列车错误:0.005556 #[0]评估错误:0.016667列车错误:0.005556 #[1]评估错误:0.005556列车错误:0
这将开始保存文件test.log中的所有内容。输出和输入。

xgboost将其日志直接打印到标准输出中,您无法更改其行为。 但是
xgb.train的
callbacks
参数能够记录与内部打印相同的时间结果

下面的代码是一个使用回调将xgboost日志记录到记录器中的示例。
log\u evaluation()
返回从xgboost internal调用的回调函数,您可以将回调函数添加到
callbacks

from logging import getLogger, basicConfig, INFO

import numpy as np
import xgboost as xgb
from sklearn.datasets import load_digits
from sklearn.model_selection import KFold

# Some logging settings
basicConfig(level=INFO)
logger = getLogger(__name__)


def log_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"""
        if len(value) == 2:
            return '%s:%g' % (value[0], value[1])
        elif len(value) == 3:
            if show_stdv:
                return '%s:%g+%g' % (value[0], value[1], value[2])
            else:
                return '%s:%g' % (value[0], value[1])
        else:
            raise ValueError("wrong metric value")

    def callback(env):
        if env.rank != 0 or len(env.evaluation_result_list) == 0 or period is False:
            return
        i = env.iteration
        if i % period == 0 or i + 1 == env.begin_iteration or i + 1 == env.end_iteration:
            msg = '\t'.join([_fmt_metric(x, show_stdv) for x in env.evaluation_result_list])
            logger.info('[%d]\t%s\n' % (i, msg))

    return callback


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)
for train_index, test_index in kf.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 performance
    watchlist = [(dtest, 'eval'), (dtrain, 'train')]
    num_round = 2
    # add logger
    callbacks = [log_evaluation(1, True)]
    bst = xgb.train(param, dtrain, num_round, watchlist, callbacks=callbacks)
看看
from logging import getLogger, basicConfig, INFO

import numpy as np
import xgboost as xgb
from sklearn.datasets import load_digits
from sklearn.model_selection import KFold

# Some logging settings
basicConfig(level=INFO)
logger = getLogger(__name__)


def log_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"""
        if len(value) == 2:
            return '%s:%g' % (value[0], value[1])
        elif len(value) == 3:
            if show_stdv:
                return '%s:%g+%g' % (value[0], value[1], value[2])
            else:
                return '%s:%g' % (value[0], value[1])
        else:
            raise ValueError("wrong metric value")

    def callback(env):
        if env.rank != 0 or len(env.evaluation_result_list) == 0 or period is False:
            return
        i = env.iteration
        if i % period == 0 or i + 1 == env.begin_iteration or i + 1 == env.end_iteration:
            msg = '\t'.join([_fmt_metric(x, show_stdv) for x in env.evaluation_result_list])
            logger.info('[%d]\t%s\n' % (i, msg))

    return callback


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)
for train_index, test_index in kf.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 performance
    watchlist = [(dtest, 'eval'), (dtrain, 'train')]
    num_round = 2
    # add logger
    callbacks = [log_evaluation(1, True)]
    bst = xgb.train(param, dtrain, num_round, watchlist, callbacks=callbacks)