Ejemplo n.º 1
0
    def cv(self, dataset: tk.data.Dataset,
           folds: tk.validation.FoldsType) -> Model:
        """CVして保存。

        Args:
            dataset: 入力データ
            folds: CVのindex

        Returns:
            self

        """
        dataset = dataset.copy()
        if self.preprocessors is not None:
            dataset.data = self.preprocessors.fit_transform(
                dataset.data, dataset.labels)
        if self.postprocessors is not None:
            dataset.labels = np.squeeze(
                self.postprocessors.fit_transform(
                    np.expand_dims(dataset.labels, axis=-1)),
                axis=-1,
            )

        self._cv(dataset, folds)
        if self.save_on_cv:
            self.save()

        return self
Ejemplo n.º 2
0
    def cv(
        self,
        dataset: tk.data.Dataset,
        folds: tk.validation.FoldsType,
        models_dir: pathlib.Path,
    ) -> dict:
        """CVして保存。

        Args:
            dataset: 入力データ
            folds: CVのindex
            models_dir: 保存先ディレクトリ (Noneなら保存しない)

        Returns:
            metrics名と値

        """
        if models_dir is not None:
            models_dir = pathlib.Path(models_dir)
            models_dir.mkdir(parents=True, exist_ok=True)

        dataset = dataset.copy()
        if self.preprocessors is not None:
            dataset.data = self.preprocessors.fit_transform(
                dataset.data, dataset.labels)
        if self.postprocessors is not None:
            dataset.labels = np.squeeze(
                self.postprocessors.fit_transform(
                    np.expand_dims(dataset.labels, axis=-1)),
                axis=-1,
            )
        scores = self._cv(dataset, folds)

        if models_dir is not None:
            if self.preprocessors is not None:
                tk.utils.dump(self.preprocessors,
                              models_dir / "preprocessors.pkl")
            if self.postprocessors is not None:
                tk.utils.dump(self.postprocessors,
                              models_dir / "postprocessors.pkl")
            self._save(models_dir)

        return scores