Source code for traveltimes_prediction.models.base_model

import abc
import numpy as np
from copy import deepcopy


[docs]class BaseModel(metaclass=abc.ABCMeta): name = None def __init__(self): self._median_imputer = None self.min_y_train = None self.max_y_train = None @abc.abstractmethod
[docs] def fit(self, X, Y): """ Method for fitting of the model. :param pd.DataFrame X: :param pd.DataFrame/pd.Series Y: :return: self """ pass
@abc.abstractmethod
[docs] def predict(self, X): """ Method for prediction of the traveltime. :param pd.Dataframe/pd.Series X: :return: float """ pass
@staticmethod @abc.abstractmethod
[docs] def load(model): """ Method for recreating the model - creating new instance. :param dict model: dumped model :return: BaseModel - instance of class derived from BaseModel """ pass
@abc.abstractmethod
[docs] def dump(self): """ Method for dumping of the model - saving all the necessary data for recreation & usage. :return: dict """ pass
def _get_descriptors(self, X): """ Method for retrieving the descriptors of the training data. Can be used for imputing during prediction phase. :param np.array X: Training data """ self._median_imputer = np.median(X, axis=0) def _impute_prediction_sample(self, X): """ Method for imputing of the data. Uses descriptors retrieved during training. :param np.array X: :return: np.array """ _X = deepcopy(X) if len(_X.shape) > 1: l = [] for item in _X: is_nan = np.isnan(item) item[is_nan] = self._median_imputer[is_nan] l.append(item) return np.array(l) else: is_nan = np.isnan(X) _X[is_nan] = self._median_imputer[is_nan] return _X def _coerce(self, val): """ Method for :param Iterable val: :return: list """ val = list(map(lambda x: x if x > self.min_y_train else self.min_y_train, val)) return val def __str__(self): return self.name def __repr__(self): return self.__str__()