Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow watchlist to be NamedTuple #199

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "XGBoost"
uuid = "009559a3-9522-5dbb-924b-0b6ed2b22bb9"
version = "2.5.0"
version = "2.5.1"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
1 change: 0 additions & 1 deletion src/XGBoost.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ include("Lib.jl")
using .Lib
using .Lib: DMatrixHandle, BoosterHandle


const LOG_LEVEL_REGEX = r"\[.*\] (\D*): "

function xgblog(s::Cstring)
Expand Down
6 changes: 3 additions & 3 deletions src/booster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ for custom loss.
"""
function update!(b::Booster, data, a...;
num_round::Integer=1,
watchlist::Any = Dict("train" => data),
watchlist=Dict("train" => data),
early_stopping_rounds::Integer=0,
maximize=false,
kw...,
Expand Down Expand Up @@ -578,7 +578,7 @@ ŷ = predict(b, dvalid, ntree_limit = b.best_iteration)
"""
function xgboost(dm::DMatrix, a...;
num_round::Integer=10,
watchlist::AbstractDict = Dict("train" => dm),
watchlist=Dict("train" => dm),
early_stopping_rounds::Integer=0,
maximize=false,
kw...
Expand All @@ -590,7 +590,7 @@ function xgboost(dm::DMatrix, a...;
# We have a watchlist - give a warning if early stopping is provided and watchlist is a Dict type with length > 1
if isa(watchlist, Dict)
if early_stopping_rounds > 0 && length(watchlist) > 1
error("You must supply an OrderedDict type for watchlist if early stopping rounds is enabled and there is more than one element in watchlist.")
error("You must supply an OrderedDict or NamedTuple type for watchlist if early stopping rounds is enabled and there is more than one element in watchlist.")
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ end
early_stopping_rounds = 2
)

watchlist_nt = (train=dtrain, eval=dtest)

bst_early_stopping = xgboost(dtrain,
num_round=30,
watchlist=watchlist_nt,
η=1,
objective="binary:logistic",
eval_metric=["rmsle","rmse"],
early_stopping_rounds = 2
)

@test XGBoost.getnrounds(bst_early_stopping) > 2
@test XGBoost.getnrounds(bst_early_stopping) <= nrounds_bst
Expand Down
Loading