abhishekkrthakur / autoxgb

XGBoost + Optuna
Apache License 2.0
653 stars 90 forks source link

Anyone knoes how to get the best params ? #14

Open Abhranta opened 2 years ago

Abhranta commented 2 years ago

Hi, I want to see the parameters of the trained model after the training is complete. Can anyone help me out with it ?

fedikadri commented 2 years ago

Since params are stored in database you can get the best using this method:

import logging
import optuna
import os
from  typing import Optional

logger = logging.getLogger(__name__)

def get_best_params(output: str) -> Optional[dict]:
    """
    Args:
        output (str): the output directory given to Autoxgb 

    Returns:
        Optional[dict]: best params if exist else None
    """

    params_path = os.path.join(output, "params.db")
    if not os.path.exists(params_path):
        logger.error(f"params doesn't exist. Invalid path: {params_path}")
        return None 

    best_params: Optional[dict] = None
    try:
        study = optuna.load_study(study_name="autoxgb", storage=f"sqlite:///{params_path}")
    except Exception as exc:
        logger.error("Error while loading optuna study from database", exc_info=exc)
    else:
        best_params = study.best_params
    finally:
        return best_params