Skip to content

Commit

Permalink
Merge pull request dmlc#690 from rcarneva/master
Browse files Browse the repository at this point in the history
modifying cv show_progress to allow print-every-n behavior
  • Loading branch information
terrytangyuan committed Dec 16, 2015
2 parents cfbf359 + 380e54a commit 4a15939
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions python-package/xgboost/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,14 @@ def mknfold(dall, nfold, param, seed, evals=(), fpreproc=None):
ret.append(CVPack(dtrain, dtest, plst))
return ret


def aggcv(rlist, show_stdv=True, show_progress=None, as_pandas=True):
def aggcv(rlist, show_stdv=True, show_progress=None, as_pandas=True, trial=0):
# pylint: disable=invalid-name
"""
Aggregate cross-validation results.
If show_progress is true, progress is displayed in every call. If
show_progress is an integer, progress will only be displayed every
`show_progress` trees, tracked via trial.
"""
cvmap = {}
idx = rlist[0].split()[0]
Expand Down Expand Up @@ -321,8 +324,6 @@ def aggcv(rlist, show_stdv=True, show_progress=None, as_pandas=True):
index.extend([k + '-mean', k + '-std'])
results.extend([mean, std])



if as_pandas:
try:
import pandas as pd
Expand All @@ -336,8 +337,9 @@ def aggcv(rlist, show_stdv=True, show_progress=None, as_pandas=True):
if show_progress is None:
show_progress = True

if show_progress:
if (isinstance(show_progress, int) and trial % show_progress == 0) or (isinstance(show_progress, bool) and show_progress):
sys.stderr.write(msg + '\n')
sys.stderr.flush()

return results

Expand Down Expand Up @@ -376,9 +378,11 @@ def cv(params, dtrain, num_boost_round=10, nfold=3, metrics=(),
as_pandas : bool, default True
Return pd.DataFrame when pandas is installed.
If False or pandas is not installed, return np.ndarray
show_progress : bool or None, default None
show_progress : bool, int, or None, default None
Whether to display the progress. If None, progress will be displayed
when np.ndarray is returned.
when np.ndarray is returned. If True, progress will be displayed at
boosting stage. If an integer is given, progress will be displayed
at every given `show_progress` boosting stage.
show_stdv : bool, default True
Whether to display the standard deviation in progress.
Results are not affected, and always contains std.
Expand Down Expand Up @@ -418,7 +422,7 @@ def cv(params, dtrain, num_boost_round=10, nfold=3, metrics=(),
fold.update(i, obj)
res = aggcv([f.eval(i, feval) for f in cvfolds],
show_stdv=show_stdv, show_progress=show_progress,
as_pandas=as_pandas)
as_pandas=as_pandas, trial=i)
results.append(res)

if early_stopping_rounds is not None:
Expand Down

0 comments on commit 4a15939

Please sign in to comment.