Add period to evaluation monitor. (#6348)
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from io import StringIO
|
||||
import numpy as np
|
||||
import os
|
||||
import xgboost as xgb
|
||||
@@ -9,29 +6,12 @@ import unittest
|
||||
import json
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import testing as tm
|
||||
|
||||
dpath = 'demo/data/'
|
||||
rng = np.random.RandomState(1994)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def captured_output():
|
||||
"""Reassign stdout temporarily in order to test printed statements
|
||||
Taken from:
|
||||
https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
|
||||
|
||||
Also works for pytest.
|
||||
|
||||
"""
|
||||
new_out, new_err = StringIO(), StringIO()
|
||||
old_out, old_err = sys.stdout, sys.stderr
|
||||
try:
|
||||
sys.stdout, sys.stderr = new_out, new_err
|
||||
yield sys.stdout, sys.stderr
|
||||
finally:
|
||||
sys.stdout, sys.stderr = old_out, old_err
|
||||
|
||||
|
||||
class TestBasic(unittest.TestCase):
|
||||
def test_compat(self):
|
||||
from xgboost.compat import lazy_isinstance
|
||||
@@ -181,7 +161,6 @@ class TestBasic(unittest.TestCase):
|
||||
assert dm.num_row() == row
|
||||
assert dm.num_col() == cols
|
||||
|
||||
|
||||
def test_cv(self):
|
||||
dm = xgb.DMatrix(dpath + 'agaricus.txt.train')
|
||||
params = {'max_depth': 2, 'eta': 1, 'verbosity': 0,
|
||||
@@ -236,7 +215,7 @@ class TestBasic(unittest.TestCase):
|
||||
print([fold.dtest.get_label() for fold in cbackenv.cvfolds])
|
||||
|
||||
# Run cross validation and capture standard out to test callback result
|
||||
with captured_output() as (out, err):
|
||||
with tm.captured_output() as (out, err):
|
||||
xgb.cv(
|
||||
params, dm, num_boost_round=1, folds=folds, callbacks=[cb],
|
||||
as_pandas=False
|
||||
@@ -257,7 +236,6 @@ class TestBasicPathLike(unittest.TestCase):
|
||||
assert dtrain.num_row() == 6513
|
||||
assert dtrain.num_col() == 127
|
||||
|
||||
|
||||
def test_DMatrix_save_to_path(self):
|
||||
"""Saving to a binary file using pathlib from a DMatrix."""
|
||||
data = np.random.randn(100, 2)
|
||||
|
||||
@@ -34,10 +34,27 @@ class TestCallbacks(unittest.TestCase):
|
||||
num_boost_round=rounds,
|
||||
evals_result=evals_result,
|
||||
verbose_eval=True)
|
||||
print('evals_result:', evals_result)
|
||||
assert len(evals_result['Train']['error']) == rounds
|
||||
assert len(evals_result['Valid']['error']) == rounds
|
||||
|
||||
with tm.captured_output() as (out, err):
|
||||
xgb.train({'objective': 'binary:logistic',
|
||||
'eval_metric': 'error'}, D_train,
|
||||
evals=[(D_train, 'Train'), (D_valid, 'Valid')],
|
||||
num_boost_round=rounds,
|
||||
evals_result=evals_result,
|
||||
verbose_eval=2)
|
||||
output: str = out.getvalue().strip()
|
||||
|
||||
pos = 0
|
||||
msg = 'Train-error'
|
||||
for i in range(rounds // 2):
|
||||
pos = output.find('Train-error', pos)
|
||||
assert pos != -1
|
||||
pos += len(msg)
|
||||
|
||||
assert output.find('Train-error', pos) == -1
|
||||
|
||||
def test_early_stopping(self):
|
||||
D_train = xgb.DMatrix(self.X_train, self.y_train)
|
||||
D_valid = xgb.DMatrix(self.X_valid, self.y_valid)
|
||||
|
||||
@@ -2,7 +2,6 @@ import collections
|
||||
import importlib.util
|
||||
import numpy as np
|
||||
import xgboost as xgb
|
||||
from xgboost.sklearn import XGBoostLabelEncoder
|
||||
import testing as tm
|
||||
import tempfile
|
||||
import os
|
||||
@@ -11,8 +10,6 @@ import pytest
|
||||
import unittest
|
||||
import json
|
||||
|
||||
from test_basic import captured_output
|
||||
|
||||
rng = np.random.RandomState(1994)
|
||||
|
||||
pytestmark = pytest.mark.skipif(**tm.no_sklearn())
|
||||
@@ -872,7 +869,7 @@ def test_parameter_validation():
|
||||
reg = xgb.XGBRegressor(foo='bar', verbosity=1)
|
||||
X = np.random.randn(10, 10)
|
||||
y = np.random.randn(10)
|
||||
with captured_output() as (out, err):
|
||||
with tm.captured_output() as (out, err):
|
||||
reg.fit(X, y)
|
||||
output = out.getvalue().strip()
|
||||
|
||||
@@ -882,7 +879,7 @@ def test_parameter_validation():
|
||||
importance_type='gain', verbosity=1)
|
||||
X = np.random.randn(10, 10)
|
||||
y = np.random.randn(10)
|
||||
with captured_output() as (out, err):
|
||||
with tm.captured_output() as (out, err):
|
||||
reg.fit(X, y)
|
||||
output = out.getvalue().strip()
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# coding: utf-8
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from io import StringIO
|
||||
from xgboost.compat import SKLEARN_INSTALLED, PANDAS_INSTALLED
|
||||
from xgboost.compat import DASK_INSTALLED
|
||||
import pytest
|
||||
@@ -281,6 +283,24 @@ class DirectoryExcursion:
|
||||
os.remove(f)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def captured_output():
|
||||
"""Reassign stdout temporarily in order to test printed statements
|
||||
Taken from:
|
||||
https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
|
||||
|
||||
Also works for pytest.
|
||||
|
||||
"""
|
||||
new_out, new_err = StringIO(), StringIO()
|
||||
old_out, old_err = sys.stdout, sys.stderr
|
||||
try:
|
||||
sys.stdout, sys.stderr = new_out, new_err
|
||||
yield sys.stdout, sys.stderr
|
||||
finally:
|
||||
sys.stdout, sys.stderr = old_out, old_err
|
||||
|
||||
|
||||
CURDIR = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
|
||||
PROJECT_ROOT = os.path.normpath(
|
||||
os.path.join(CURDIR, os.path.pardir, os.path.pardir))
|
||||
|
||||
Reference in New Issue
Block a user