Ejemplo n.º 1
0
def test_handling_alias_metrics() -> None:

    for alias in [
            "lambdarank",
            "rank_xendcg",
            "xendcg",
            "xe_ndcg",
            "xe_ndcg_mart",
            "xendcg_mart",
            "ndcg",
    ]:
        lgbm_params = {"metric": alias}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params["metric"] == "ndcg"

    for alias in ["mean_average_precision", "map"]:
        lgbm_params = {"metric": alias}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params["metric"] == "map"

    lgbm_params = {}
    _handling_alias_metrics(lgbm_params)
    assert lgbm_params == {}

    lgbm_params = {"metric": "auc"}
    _handling_alias_metrics(lgbm_params)
    assert lgbm_params["metric"] == "auc"

    lgbm_params = {"metric": "rmse"}
    _handling_alias_metrics(lgbm_params)
    assert lgbm_params["metric"] == "rmse"
Ejemplo n.º 2
0
    def __init__(self, lgbm_params=None, lgbm_kwargs=None):
        # type: (Dict[str, Any], Dict[str,Any]) -> None

        # Handling alias metrics.
        if lgbm_params is not None:
            _handling_alias_metrics(lgbm_params)

        self.lgbm_params = lgbm_params or {}
        self.lgbm_kwargs = lgbm_kwargs or {}
Ejemplo n.º 3
0
def test_handling_alias_metrics(aliases: List[str], expect: str) -> None:
    if len(aliases) > 0:
        for alias in aliases:
            lgbm_params = {"metric": alias}
            _handling_alias_metrics(lgbm_params)
            assert lgbm_params["metric"] == expect
    else:
        lgbm_params = {}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params == {}
Ejemplo n.º 4
0
    def __init__(
        self,
        lgbm_params: Optional[Dict[str, Any]] = None,
        lgbm_kwargs: Optional[Dict[str, Any]] = None,
    ) -> None:

        # Handling alias metrics.
        if lgbm_params is not None:
            _handling_alias_metrics(lgbm_params)

        self.lgbm_params = lgbm_params or {}
        self.lgbm_kwargs = lgbm_kwargs or {}
Ejemplo n.º 5
0
    def __init__(
        self,
        params: Dict[str, Any],
        train_set: "lgb.Dataset",
        num_boost_round: int = 1000,
        fobj: Optional[Callable[..., Any]] = None,
        feval: Optional[Callable[..., Any]] = None,
        feature_name: str = "auto",
        categorical_feature: str = "auto",
        early_stopping_rounds: Optional[int] = None,
        verbose_eval: Optional[Union[bool, int]] = True,
        callbacks: Optional[List[Callable[..., Any]]] = None,
        time_budget: Optional[int] = None,
        sample_size: Optional[int] = None,
        study: Optional[optuna.study.Study] = None,
        optuna_callbacks: Optional[List[Callable[[Study, FrozenTrial],
                                                 None]]] = None,
        verbosity: Optional[int] = None,
        show_progress_bar: bool = True,
    ) -> None:

        _imports.check()

        params = copy.deepcopy(params)

        # Handling alias metrics.
        _handling_alias_metrics(params)

        args = [params, train_set]
        kwargs = dict(
            num_boost_round=num_boost_round,
            fobj=fobj,
            feval=feval,
            feature_name=feature_name,
            categorical_feature=categorical_feature,
            early_stopping_rounds=early_stopping_rounds,
            verbose_eval=verbose_eval,
            callbacks=callbacks,
            time_budget=time_budget,
            sample_size=sample_size,
            verbosity=verbosity,
            show_progress_bar=show_progress_bar,
        )  # type: Dict[str, Any]
        self._parse_args(*args, **kwargs)
        self._start_time = None  # type: Optional[float]
        self._optuna_callbacks = optuna_callbacks
        self._best_params = {}

        # Set default parameters as best.
        self._best_params.update(_DEFAULT_LIGHTGBM_PARAMETERS)

        if study is None:
            self.study = optuna.create_study(
                direction="maximize" if self.higher_is_better(
                ) else "minimize")
        else:
            self.study = study

        if self.higher_is_better():
            if self.study.direction != optuna.study.StudyDirection.MAXIMIZE:
                metric_name = self.lgbm_params.get("metric", "binary_logloss")
                raise ValueError(
                    "Study direction is inconsistent with the metric {}. "
                    "Please set 'maximize' as the direction.".format(
                        metric_name))
        else:
            if self.study.direction != optuna.study.StudyDirection.MINIMIZE:
                metric_name = self.lgbm_params.get("metric", "binary_logloss")
                raise ValueError(
                    "Study direction is inconsistent with the metric {}. "
                    "Please set 'minimize' as the direction.".format(
                        metric_name))

        if verbosity is not None:
            warnings.warn(
                "`verbosity` argument is deprecated and will be removed in the future. "
                "The removal of this feature is currently scheduled for v4.0.0, "
                "but this schedule is subject to change. Please use optuna.logging.set_verbosity()"
                " instead.",
                FutureWarning,
            )
Ejemplo n.º 6
0
    def __init__(
        self,
        params: Dict[str, Any],
        train_set: "lgb.Dataset",
        num_boost_round: int = 1000,
        fobj: Optional[Callable[..., Any]] = None,
        feval: Optional[Callable[..., Any]] = None,
        feature_name: str = "auto",
        categorical_feature: str = "auto",
        early_stopping_rounds: Optional[int] = None,
        verbose_eval: Optional[Union[bool, int]] = True,
        callbacks: Optional[List[Callable[..., Any]]] = None,
        time_budget: Optional[int] = None,
        sample_size: Optional[int] = None,
        study: Optional[optuna.study.Study] = None,
        optuna_callbacks: Optional[List[Callable[[Study, FrozenTrial], None]]] = None,
        verbosity: Optional[int] = None,
        show_progress_bar: bool = True,
        model_dir: Optional[str] = None,
    ) -> None:

        _imports.check()

        params = copy.deepcopy(params)

        # Handling alias metrics.
        _handling_alias_metrics(params)

        args = [params, train_set]
        kwargs: Dict[str, Any] = dict(
            num_boost_round=num_boost_round,
            fobj=fobj,
            feval=feval,
            feature_name=feature_name,
            categorical_feature=categorical_feature,
            early_stopping_rounds=early_stopping_rounds,
            verbose_eval=verbose_eval,
            callbacks=callbacks,
            time_budget=time_budget,
            sample_size=sample_size,
            verbosity=verbosity,
            show_progress_bar=show_progress_bar,
        )
        self._parse_args(*args, **kwargs)
        self._start_time: Optional[float] = None
        self._optuna_callbacks = optuna_callbacks
        self._best_booster_with_trial_number: Optional[
            Tuple[Union[lgb.Booster, lgb.CVBooster], int]
        ] = None
        self._model_dir = model_dir

        # Should not alter data since `min_data_in_leaf` is tuned.
        # https://lightgbm.readthedocs.io/en/latest/Parameters.html#feature_pre_filter
        if self.lgbm_params.get("feature_pre_filter", False):
            warnings.warn(
                "feature_pre_filter is given as True but will be set to False. This is required "
                "for the tuner to tune min_data_in_leaf."
            )
        self.lgbm_params["feature_pre_filter"] = False

        if study is None:
            self.study = optuna.create_study(
                direction="maximize" if self.higher_is_better() else "minimize"
            )
        else:
            self.study = study

        if self.higher_is_better():
            if self.study.direction != optuna.study.StudyDirection.MAXIMIZE:
                metric_name = self.lgbm_params.get("metric", "binary_logloss")
                raise ValueError(
                    "Study direction is inconsistent with the metric {}. "
                    "Please set 'maximize' as the direction.".format(metric_name)
                )
        else:
            if self.study.direction != optuna.study.StudyDirection.MINIMIZE:
                metric_name = self.lgbm_params.get("metric", "binary_logloss")
                raise ValueError(
                    "Study direction is inconsistent with the metric {}. "
                    "Please set 'minimize' as the direction.".format(metric_name)
                )

        if verbosity is not None:
            warnings.warn(
                "`verbosity` argument is deprecated and will be removed in the future. "
                "The removal of this feature is currently scheduled for v4.0.0, "
                "but this schedule is subject to change. Please use optuna.logging.set_verbosity()"
                " instead.",
                FutureWarning,
            )
Ejemplo n.º 7
0
    def __init__(
        self,
        params: Dict[str, Any],
        train_set: "lgb.Dataset",
        num_boost_round: int = 1000,
        fobj: Optional[Callable[..., Any]] = None,
        feval: Optional[Callable[..., Any]] = None,
        feature_name: str = "auto",
        categorical_feature: str = "auto",
        early_stopping_rounds: Optional[int] = None,
        verbose_eval: Optional[Union[bool, int]] = True,
        callbacks: Optional[List[Callable[..., Any]]] = None,
        time_budget: Optional[int] = None,
        sample_size: Optional[int] = None,
        study: Optional[optuna.study.Study] = None,
        optuna_callbacks: Optional[List[Callable[[Study, FrozenTrial],
                                                 None]]] = None,
        verbosity: Optional[int] = 1,
    ) -> None:

        _imports.check()

        params = copy.deepcopy(params)

        # Handling alias metrics.
        _handling_alias_metrics(params)

        args = [params, train_set]
        kwargs = dict(
            num_boost_round=num_boost_round,
            fobj=fobj,
            feval=feval,
            feature_name=feature_name,
            categorical_feature=categorical_feature,
            early_stopping_rounds=early_stopping_rounds,
            verbose_eval=verbose_eval,
            callbacks=callbacks,
            time_budget=time_budget,
            sample_size=sample_size,
            verbosity=verbosity,
        )  # type: Dict[str, Any]
        self._parse_args(*args, **kwargs)
        self._start_time = None  # type: Optional[float]
        self._use_pbar = True
        self._optuna_callbacks = optuna_callbacks
        self._best_params = {}

        # Set default parameters as best.
        self._best_params.update(_DEFAULT_LIGHTGBM_PARAMETERS)

        if study is None:
            self.study = optuna.create_study(
                direction="maximize" if self.higher_is_better(
                ) else "minimize")
        else:
            self.study = study

        if self.higher_is_better():
            if self.study.direction != optuna.study.StudyDirection.MAXIMIZE:
                metric_name = self.lgbm_params.get("metric", "binary_logloss")
                raise ValueError(
                    "Study direction is inconsistent with the metric {}. "
                    "Please set 'maximize' as the direction.".format(
                        metric_name))
        else:
            if self.study.direction != optuna.study.StudyDirection.MINIMIZE:
                metric_name = self.lgbm_params.get("metric", "binary_logloss")
                raise ValueError(
                    "Study direction is inconsistent with the metric {}. "
                    "Please set 'minimize' as the direction.".format(
                        metric_name))
Ejemplo n.º 8
0
def test_handling_alias_metrics() -> None:

    for alias in [
            "lambdarank",
            "rank_xendcg",
            "xendcg",
            "xe_ndcg",
            "xe_ndcg_mart",
            "xendcg_mart",
            "ndcg",
    ]:
        lgbm_params = {"metric": alias}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params["metric"] == "ndcg"

    for alias in ["mean_average_precision", "map"]:
        lgbm_params = {"metric": alias}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params["metric"] == "map"

    for alias in [
            "regression", "regression_l2", "l2", "mean_squared_error", "mse"
    ]:
        lgbm_params = {"metric": alias}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params["metric"] == "l2"

    lgbm_params = {}
    _handling_alias_metrics(lgbm_params)
    assert lgbm_params == {}

    lgbm_params = {"metric": "auc"}
    _handling_alias_metrics(lgbm_params)
    assert lgbm_params["metric"] == "auc"

    for alias in ["l2_root", "root_mean_squared_error", "rmse"]:
        lgbm_params = {"metric": alias}
        _handling_alias_metrics(lgbm_params)
        assert lgbm_params["metric"] == "rmse"