'suggest_int() missing 1 required positional argument: 'high' error on Optuna

I have the following code of Optuna to do the hyperparameter tunning for a Xgboost classifier.

import optuna 
from optuna import Trial, visualization
from optuna.samplers import TPESampler
from xgboost import XGBClassifier

def objective(trial: Trial,X_train,y_train,X_test,y_test):
    
    param = {
            "n_estimators" : Trial.suggest_int("n_estimators", 0, 1000),
            'max_depth':Trial.suggest_int('max_depth', 2, 25),
            'reg_alpha':Trial.suggest_int('reg_alpha', 0, 5),
            'reg_lambda':Trial.suggest_int('reg_lambda', 0, 5),
            'min_child_weight':Trial.suggest_int('min_child_weight', 0, 5),
            'gamma':Trial.suggest_int('gamma', 0, 5),
            'learning_rate':Trial.suggest_loguniform('learning_rate',0.005,0.5),
            'colsample_bytree':Trial.suggest_discrete_uniform('colsample_bytree',0.1,1,0.01),
            'nthread' : -1
            }
    
    model = XGBClassifier(**param)

    model.fit(X_train,y_train)

    return cross_val_score(model,X_test,y_test).mean()

study = optuna.create_study(direction='maximize',sampler=TPESampler())
study.optimize(lambda trial : objective(trial,X_train,y_train,X_test,y_test),n_trials= 50)

It keeps giving me the following error:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\JaneStreet\lib\site-packages\optuna\_optimize.py", line 217, in _run_trial
    value_or_values = func(trial)
  File "<ipython-input-74-c1454daaa53e>", line 2, in <lambda>
    study.optimize(lambda trial : objective(trial,X_train,y_train,X_test,y_test),n_trials= 50)
  File "<ipython-input-73-4438e1db47ef>", line 4, in objective
    "n_estimators" : Trial.suggest_int("n_estimators", 0, 1000),
TypeError: suggest_int() missing 1 required positional argument: 'high'

Thanks so much



Solution 1:[1]

The problem is that you are calling suggest_int on the class Trial as if it were a class/static method. suggest_int is a regular method and should be called on an object, in this case trial. Changing Trial.suggest_int to trial.suggest_int should get rid of the error.

Solution 2:[2]

What about below. I just changed the params after objective and changed Trial to trial.

def objective(trial,X_train,y_train,X_test,y_test):
    
    param = {
            "n_estimators" : trial.suggest_int("n_estimators", 0, 1000),
            'max_depth':trial.suggest_int('max_depth', 2, 25),
            'reg_alpha':trial.suggest_int('reg_alpha', 0, 5),
            'reg_lambda':trial.suggest_int('reg_lambda', 0, 5),
            'min_child_weight':trial.suggest_int('min_child_weight', 0, 5),
            'gamma':trial.suggest_int('gamma', 0, 5),
            'learning_rate':trial.suggest_loguniform('learning_rate',0.005,0.5),
            'colsample_bytree':trial.suggest_discrete_uniform('colsample_bytree',0.1,1,0.01),
            'nthread' : -1
            }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hvy
Solution 2 J R