LoginSignup
0
0

XGBoost を hyperopt にてハイパーパラメータチューニングする際に起きる `Invalid Parameter format for max_depth expect int but value='#.#'`エラーへの対応方法

Last updated at Posted at 2023-07-06

概要

XGBoost を hyperopt にてハイパーパラメータチューニングする際に起きる Invalid Parameter format for max_depth expect int but value='#.#'エラーへの対応方法を共有します。

hp.quniformメソッドにより、max_depthをチューニングする場合に、int 型でなく、float 型の数値が渡されることで発生するエラーのようです。

from hyperopt import hp, tpe, fmin, STATUS_OK, Trials

space = {
    "max_depth": hp.quniform("max_depth", 2, 5, 1),
}

trials = Trials()
fmin(
    objective,
    space=space,
    algo=tpe.suggest,
    max_evals=30,
    trials=trials,
)
XGBoostError: Invalid Parameter format for max_depth expect int but value='4.0'

image.png

対応方法については、次の issues にて、hp.choiceメソッドとnp.arangeメソッドを組み合わせる方法が紹介されておりました。本記事では、実際の対応例を提示します。

hyperopt.pyll.basescope.intメソッドを利用することもで解決できるようです。

from hyperopt import hp, tpe, fmin, STATUS_OK, Trials
from hyperopt.pyll.base import scope

space = {
    "max_depth": scope.int(hp.quniform("max_depth", 2, 5, 1)),
}

対応方法

エラーが発生するコード

from hyperopt import hp, tpe, fmin, STATUS_OK, Trials

space = {
    "max_depth": hp.quniform("max_depth", 2, 5, 1),
}

trials = Trials()
fmin(
    objective,
    space=space,
    algo=tpe.suggest,
    max_evals=30,
    trials=trials,
)

image.png

エラーへの対応後のコード 対応方法案1

from hyperopt import hp, tpe, fmin, STATUS_OK, Trials
import numpy as np
 
space = {
    "max_depth": hp.choice("max_depth", np.arange(2, 10, dtype=int)),
}

trials = Trials()
fmin(
    objective,
    space=space,
    algo=tpe.suggest,
    max_evals=30,
    trials=trials,
)

image.png

エラーへの対応後のコード 対応方法案2

from hyperopt import hp, tpe, fmin, STATUS_OK, Trials
from hyperopt.pyll.base import scope

space = {
    "max_depth": scope.int(hp.quniform("max_depth", 2, 5, 1)),
}

trials = Trials()
fmin(
    objective,
    space=space,
    algo=tpe.suggest,
    max_evals=30,
    trials=trials,
)

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0