xgboost/demo/guide-python/evals_result.py
Jiaming Yuan 1f9a57d17b
[Breaking] Require format to be specified in input URI. (#9077)
Previously, we use `libsvm` as default when format is not specified. However, the dmlc
data parser is not particularly robust against errors, and the most common type of error
is undefined format.

Along with which, we will recommend users to use other data loader instead. We will
continue the maintenance of the parsers as it's currently used for many internal tests
including federated learning.
2023-04-28 19:45:15 +08:00

44 lines
1.1 KiB
Python

"""
This script demonstrate how to access the eval metrics
======================================================
"""
import os
import xgboost as xgb
CURRENT_DIR = os.path.dirname(__file__)
dtrain = xgb.DMatrix(
os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
)
dtest = xgb.DMatrix(
os.path.join(CURRENT_DIR, "../data/agaricus.txt.test?format=libsvm")
)
param = [
("max_depth", 2),
("objective", "binary:logistic"),
("eval_metric", "logloss"),
("eval_metric", "error"),
]
num_round = 2
watchlist = [(dtest, "eval"), (dtrain, "train")]
evals_result = {}
bst = xgb.train(param, dtrain, num_round, watchlist, evals_result=evals_result)
print("Access logloss metric directly from evals_result:")
print(evals_result["eval"]["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 dictionary:")
print(evals_result)