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
def predict(self, dataset: tk.data.Dataset) -> typing.List[np.ndarray]: """予測結果をリストで返す。 Args: dataset (tk.data.Dataset): 入力データ Returns: len(self.folds)個の予測結果 """ dataset = dataset.copy() if self.preprocessors is not None: dataset.data = self.preprocessors.transform(dataset.data) pred_list = self._predict(dataset) if self.postprocessors is not None: for i in range(len(pred_list)): if pred_list[i].ndim <= 1: pred_list[i] = np.squeeze( self.postprocessors.inverse_transform( np.expand_dims(pred_list[i], axis=-1)), axis=-1, ) else: pred_list[i] = self.postprocessors.inverse_transform( pred_list[i]) return pred_list
def predict(self, dataset: tk.data.Dataset, fold: int) -> np.ndarray: """推論結果を返す。 Args: dataset: 入力データ fold: 使用するモデル Returns: 推論結果 """ dataset = dataset.copy() if self.preprocessors is not None: dataset.data = self.preprocessors.transform(dataset.data) pred = self._predict(dataset, fold) if self.postprocessors is not None: if isinstance(pred, np.ndarray) and pred.ndim <= 1: pred = np.squeeze( self.postprocessors.inverse_transform( np.expand_dims(pred, axis=-1)), axis=-1, ) else: pred = self.postprocessors.inverse_transform(pred) return pred
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