当前位置: 首页 > 工具软件 > Evals > 使用案例 >

python_xgboost例03_evals_result评估结果

郎恺
2023-12-01
%matplotlib inline

本专栏所有例题数据放在 网址[https://download.csdn.net/download/u012338969/85439555(https://download.csdn.net/download/u012338969/85439555)

This script demonstrate how to access the eval metrics

import os
import xgboost as xgb
dtrain = xgb.DMatrix( './data/agaricus.txt.train')
dtest = xgb.DMatrix( './data/agaricus.txt.test')
param = [('max_depth', 2), ('objective', 'binary:logistic'), ('eval_metric', 'logloss'), ('eval_metric', 'error')]

num_round = 10
watchlist = [(dtest,'eval'), (dtrain,'train')]

evals_result = {}
bst = xgb.train(param, dtrain, num_round, watchlist, evals_result=evals_result)
[0]	eval-logloss:0.48039	eval-error:0.04283	train-logloss:0.48254	train-error:0.04652
[1]	eval-logloss:0.35776	eval-error:0.04283	train-logloss:0.35954	train-error:0.04652
[2]	eval-logloss:0.28009	eval-error:0.02483	train-logloss:0.27993	train-error:0.02334
[3]	eval-logloss:0.21606	eval-error:0.04035	train-logloss:0.21860	train-error:0.04161
[4]	eval-logloss:0.17013	eval-error:0.01924	train-logloss:0.17063	train-error:0.01735
[5]	eval-logloss:0.14026	eval-error:0.02173	train-logloss:0.13910	train-error:0.02226
[6]	eval-logloss:0.12097	eval-error:0.01552	train-logloss:0.11891	train-error:0.01336
[7]	eval-logloss:0.10550	eval-error:0.02731	train-logloss:0.10227	train-error:0.02150
[8]	eval-logloss:0.09550	eval-error:0.02731	train-logloss:0.09150	train-error:0.02150
[9]	eval-logloss:0.08723	eval-error:0.02731	train-logloss:0.08313	train-error:0.02150


D:\d_programe\Anaconda3\lib\site-packages\xgboost\core.py:525: FutureWarning: Pass `evals` as keyword args.  Passing these as positional arguments will be considered as error in future releases.
  warnings.warn(
print('Access logloss metric directly from evals_result:')
print(evals_result['eval']['logloss'])
Access logloss metric directly from evals_result:
[0.4803859960359494, 0.35775544743668136, 0.28009184384701197, 0.21605562400662479, 0.1701263918926966, 0.14025616379423514, 0.12096623357207341, 0.10550202664004062, 0.09549925424283444, 0.08723436453808064]
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)
Access metrics through a loop:
- eval
   - logloss
      - [0.4803859960359494, 0.35775544743668136, 0.28009184384701197, 0.21605562400662479, 0.1701263918926966, 0.14025616379423514, 0.12096623357207341, 0.10550202664004062, 0.09549925424283444, 0.08723436453808064]
   - error
      - [0.04283054003724395, 0.04283054003724395, 0.02482929857231533, 0.04034761018001241, 0.01924270639354438, 0.02172563625077592, 0.01551831160769708, 0.02731222842954686, 0.02731222842954686, 0.02731222842954686]
- train
   - logloss
      - [0.4825405066196003, 0.3595360331769255, 0.27993453837989496, 0.2185990637634164, 0.17062663876404507, 0.13910131798203448, 0.11891032012147178, 0.10226806316741524, 0.09149656920272982, 0.08312625405458002]
   - error
      - [0.04652233993551359, 0.04652233993551359, 0.02333793950560418, 0.04160908951328113, 0.01734991555350837, 0.02226316597574083, 0.0133578995854445, 0.02149547059726701, 0.02149547059726701, 0.02149547059726701]

Access complete dictionary:
{'eval': OrderedDict([('logloss', [0.4803859960359494, 0.35775544743668136, 0.28009184384701197, 0.21605562400662479, 0.1701263918926966, 0.14025616379423514, 0.12096623357207341, 0.10550202664004062, 0.09549925424283444, 0.08723436453808064]), ('error', [0.04283054003724395, 0.04283054003724395, 0.02482929857231533, 0.04034761018001241, 0.01924270639354438, 0.02172563625077592, 0.01551831160769708, 0.02731222842954686, 0.02731222842954686, 0.02731222842954686])]), 'train': OrderedDict([('logloss', [0.4825405066196003, 0.3595360331769255, 0.27993453837989496, 0.2185990637634164, 0.17062663876404507, 0.13910131798203448, 0.11891032012147178, 0.10226806316741524, 0.09149656920272982, 0.08312625405458002]), ('error', [0.04652233993551359, 0.04652233993551359, 0.02333793950560418, 0.04160908951328113, 0.01734991555350837, 0.02226316597574083, 0.0133578995854445, 0.02149547059726701, 0.02149547059726701, 0.02149547059726701])])}

 类似资料: