コード例 #1
0
    def get_best_booster(self) -> "lgb.CVBooster":
        """Return the best cvbooster.

        If the best booster cannot be found, :class:`ValueError` will be raised.
        To prevent the errors, please save boosters by specifying
        both of the ``model_dir`` and the ``return_cvbooster`` arguments of
        :meth:`~optuna.integration.lightgbm.LightGBMTunerCV.__init__`,
        when you resume tuning or you run tuning in parallel.
        """
        if self.lgbm_kwargs.get("return_cvbooster") is not True:
            raise ValueError(
                "LightGBMTunerCV requires `return_cvbooster=True` for method `get_best_booster()`."
            )
        if self._best_booster_with_trial_number is not None:
            if self._best_booster_with_trial_number[
                    1] == self.study.best_trial.number:
                return self._best_booster_with_trial_number[0]
        if len(self.study.trials) == 0:
            raise ValueError(
                "The best booster is not available because no trials completed."
            )

        # The best booster exists, but this instance does not have it.
        # This may be due to resuming or parallelization.
        if self._model_dir is None:
            raise ValueError(
                "The best booster cannot be found. It may be found in the other processes due to "
                "resuming or distributed computing. Please set the `model_dir` argument of "
                "`LightGBMTunerCV.__init__` and make sure that boosters are shared with all "
                "processes.")

        best_trial = self.study.best_trial
        path = os.path.join(self._model_dir,
                            "{}.pkl".format(best_trial.number))
        if not os.path.exists(path):
            raise ValueError(
                "The best booster cannot be found in {}. If you execute `LightGBMTunerCV` in "
                "distributed environment, please use network file system (e.g., NFS) to share "
                "models with multiple workers.".format(self._model_dir))

        with open(path, "rb") as fin:
            boosters, best_iteration = pickle.load(fin)
            # At version `lightgbm==3.0.0`, :class:`lightgbm.CVBooster` does not
            # have `__getstate__` which is required for pickle serialization.
            cvbooster = lgb.CVBooster()
            cvbooster.boosters = boosters
            cvbooster.best_iteration = best_iteration

        return cvbooster
コード例 #2
0
# ベースモデル読み込み
with open(f"./pkl/lr_{hash_lr}.pkl", "rb") as f:
    clf_lr = pickle.load(f)
with open(f"./pkl/rf_{hash_rf}.pkl", "rb") as f:
    clf_rf = pickle.load(f)
with open(f"./pkl/knn_{hash_knn}.pkl", "rb") as f:
    clf_knn = pickle.load(f)
# with open(f"./pkl/lgbm_{hash_lgbm}.pkl", "rb") as f:
#     clf_lgbm = pickle.load(f)
with open(f"./pkl/catb_{hash_catb}.pkl", "rb") as f:
    clf_catb = pickle.load(f)
with open(f"./pkl/svc_{hash_svc}.pkl", "rb") as f:
    clf_svc = pickle.load(f)

cvbooster = lgb.CVBooster()
for i in range(5):
    booster_file = f"./pkl/lgbm_cv_{hash_lgbm}_{i}.txt"
    tmp = lgb.Booster(model_file=booster_file)
    cvbooster._append(booster=tmp)
with open(f"./pkl/lgbm_cv_{hash_lgbm}_best_iter.txt") as f:
    cvbooster.best_iteration = int(f.read())

net = mlp_net.net()
net.load_state_dict(torch.load(f"./pkl/mlp_{hash_mlp}.net"))

# データ読み込み
df_test_tree = pd.read_csv("./data/test_prep_tree.csv")
df_test_nontree = pd.read_csv("./data/test_prep_nontree.csv")

df_lr = pd.read_csv(f"./output/base_pred_proba_lr_{hash_lr}.csv")