Skip to content

Commit

Permalink
add tests for arbitrary fold indices
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Laslett committed Jun 4, 2018
1 parent 97be08d commit 2b87e24
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/python/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
import sys
from contextlib import contextmanager
from io import StringIO
import numpy as np
import xgboost as xgb
import unittest
Expand All @@ -8,6 +11,21 @@
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
"""
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_basic(self):
Expand Down Expand Up @@ -238,3 +256,42 @@ def test_cv_no_shuffle(self):
cv = xgb.cv(params, dm, num_boost_round=10, shuffle=False, nfold=10, as_pandas=False)
assert isinstance(cv, dict)
assert len(cv) == (4)

def test_cv_explicit_fold_indices(self):
dm = xgb.DMatrix(dpath + 'agaricus.txt.train')
params = {'max_depth': 2, 'eta': 1, 'silent': 1, 'objective': 'binary:logistic'}
folds = [
# Train Test
([1, 3], [5, 8]),
([7, 9], [23, 43]),
]

# return np.ndarray
with captured_output() as (out, err):
cv = xgb.cv(params, dm, num_boost_round=10, folds=folds, as_pandas=False)
print('Hello World!')
output = out.getvalue().strip()
assert output == 'Hello World!'
assert isinstance(cv, dict)
assert len(cv) == (4)

def test_cv_explicit_fold_indices_labels(self):
params = {'max_depth': 2, 'eta': 1, 'silent': 1, 'objective': 'reg:linear'}
N = 100
F = 3
dm = xgb.DMatrix(data=np.random.randn(N, F), label=np.arange(N))
folds = [
# Train Test
([1, 3], [5, 8]),
([7, 9], [23, 43, 11]),
]

# Use callback to log the test labels in each fold
cb = lambda cbackenv: print([fold.dtest.get_label() for fold in cbackenv.cvfolds])
with captured_output() as (out, err):
xgb.cv(
params, dm, num_boost_round=1, folds=folds, callbacks=[cb],
as_pandas=False
)
output = out.getvalue().strip()
assert output == '[array([5., 8.], dtype=float32), array([23., 43., 11.], dtype=float32)]'

0 comments on commit 2b87e24

Please sign in to comment.