.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python/examples/sklearn_evals_result.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_examples_sklearn_evals_result.py: Demo for accessing the xgboost eval metrics by using sklearn interface ====================================================================== .. GENERATED FROM PYTHON SOURCE LINES 5-51 .. code-block:: Python import numpy as np from sklearn.datasets import make_hastie_10_2 import xgboost as xgb X, y = make_hastie_10_2(n_samples=2000, random_state=42) # Map labels from {-1, 1} to {0, 1} labels, y = np.unique(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", ) # Or you can use: clf = xgb.XGBClassifier(**param_dist) clf.fit( X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)], verbose=True, ) # Load evals result by calling the evals_result() function 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) .. _sphx_glr_download_python_examples_sklearn_evals_result.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sklearn_evals_result.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sklearn_evals_result.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: sklearn_evals_result.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_