Source code for optilab.functions.surrogate.polynomial_regression

"""
Surrogate objective function which approximates the function value
with polynomial regression with interactions optimized using least squares.
"""

import numpy as np
from sklearn.preprocessing import PolynomialFeatures

from ...data_classes import Point, PointList
from .surrogate_objective_function import SurrogateObjectiveFunction


[docs] class PolynomialRegression(SurrogateObjectiveFunction): """ Surrogate objective function which approximates the function value with polynomial regression with interactions optimized using least squares. """
[docs] def __init__( self, degree: int, train_set: PointList | None = None, ) -> None: """ Class constructor. Args: degree: Degree of the polynomial used for approximation. train_set: Training data for the model. """ self.preprocessor = PolynomialFeatures(degree=degree) super().__init__( f"polynomial_regression_{degree}_degree", train_set, {"degree": degree}, )
[docs] def train(self, train_set: PointList) -> None: """ Train the Surrogate function with provided data Args: train_set: Train data for the model. """ super().train(train_set) x, y = self.train_set.pairs() self.weights = np.linalg.lstsq(self.preprocessor.fit_transform(x), y)[0]
[docs] def __call__(self, point: Point) -> Point: """ Estimate the value of a single point with the surrogate function. Args: point: Point to estimate. Raises: ValueError: If dimensionality of x doesn't match self.dim. Returns: Estimated value of the function in the provided point. """ super().__call__(point) return Point( x=point.x, y=sum(self.weights * self.preprocessor.fit_transform([point.x])[0]), is_evaluated=False, )