Demo for accessing the xgboost eval metrics by using sklearn interface
2000
,
random_state
=
42
)
labels
,
y
=
(
y
,
return_inverse
=
True
)
X_train
,
X_test
=
X
[:
1600
],
X
[
1600
:]
y_train
,
y_test
=
y
[:
1600
],
y
[
1600
:]
param_dist
=
{
"objective"
:
"binary:logistic"
,
"n_estimators"
:
2
}
clf
=
xgb
.
XGBModel
(
**
param_dist
,
eval_metric
=
"logloss"
,
)
clf
.
fit
(
X_train
,
y_train
,
eval_set
=
[(
X_train
,
y_train
),
(
X_test
,
y_test
)],
verbose
=
True
,
)
evals_result
=
clf
.
evals_result
()
print
(
"Access logloss metric directly from validation_0:"
)
print
(
evals_result
[
"validation_0"
][
"logloss"
])
print
(
""
)
print
(
"Access metrics through a loop:"
)
for
e_name
,
e_mtrs
in
evals_result
.
items
():
print
(
"-
{}
"
.
format
(
e_name
))
for
e_mtr_name
,
e_mtr_vals
in
e_mtrs
.
items
():
print
(
" -
{}
"
.
format
(
e_mtr_name
))
print
(
" -
{}
"
.
format
(
e_mtr_vals
))
print
(
""
)
print
(
"Access complete dict:"
)
print
(
evals_result
)