Source code for pysgmcmc.models.base_model

import abc
import numpy as np


[docs]class BaseModel(object): __metaclass__ = abc.ABCMeta
[docs] def __init__(self): """ Abstract base class for all models """ self.X = None self.y = None
@abc.abstractmethod
[docs] def train(self, X, y): """ Trains the model on the provided data. Parameters ---------- X: np.ndarray (N, D) Input data points. The dimensionality of X is (N, D), with N as the number of points and D is the number of input dimensions. y: np.ndarray (N,) The corresponding target values of the input data points. """ pass
[docs] def update(self, X, y): """ Update the model with the new additional data. Override this function if your model allows to do something smarter than simple retraining Parameters ---------- X: np.ndarray (N, D) Input data points. The dimensionality of X is (N, D), with N as the number of points and D is the number of input dimensions. y: np.ndarray (N,) The corresponding target values of the input data points. """ X = np.append(self.X, X, axis=0) y = np.append(self.y, y, axis=0) self.train(X, y)
@abc.abstractmethod
[docs] def predict(self, X_test): """ Predicts for a given set of test data points the mean and variance of its target values Parameters ---------- X_test: np.ndarray (N, D) N Test data points with input dimensions D Returns ---------- mean: ndarray (N,) Predictive mean of the test data points var: ndarray (N,) Predictive variance of the test data points """ pass
def _check_shapes_train(func: callable): def func_wrapper(self, X, y, *args, **kwargs): assert X.shape[0] == y.shape[0] assert len(X.shape) == 2 assert len(y.shape) == 1 return func(self, X, y, *args, **kwargs) return func_wrapper def _check_shapes_predict(func: callable): def func_wrapper(self, X, *args, **kwargs): assert len(X.shape) == 2 return func(self, X, *args, **kwargs) return func_wrapper
[docs] def get_json_data(self): """ Json getter function' Returns ---------- dictionary """ json_data = {'X': self.X if self.X is None else self.X.tolist(), 'y': self.y if self.y is None else self.y.tolist(), 'hyperparameters': ""} return json_data
[docs] def get_incumbent(self): """ Returns the best observed point and its function value Returns ---------- incumbent: ndarray (D,) current incumbent incumbent_value: ndarray (N,) the observed value of the incumbent """ best_idx = np.argmin(self.y) return self.X[best_idx], self.y[best_idx]
def zero_one_normalization(X, lower=None, upper=None): if lower is None: lower = np.min(X, axis=0) if upper is None: upper = np.max(X, axis=0) X_normalized = np.true_divide((X - lower), (upper - lower)) return X_normalized, lower, upper def zero_one_unnormalization(X_normalized, lower, upper): return lower + (upper - lower) * X_normalized def zero_mean_unit_var_normalization(X, mean=None, std=None): if mean is None: mean = np.mean(X, axis=0) if std is None: std = np.std(X, axis=0) X_normalized = (X - mean) / std return X_normalized, mean, std def zero_mean_unit_var_unnormalization(X_normalized, mean, std): return X_normalized * std + mean