Predictor#

class openstef_core.mixins.Predictor[source]#

Bases: Stateful, Generic

Abstract base class for prediction models.

This class provides the basic interface for models that can be fitted to data of type I and then used to generate predictions of type O. It follows the scikit-learn pattern with separate fit and predict phases, and includes state management capabilities.

Type parameters:

I: The input data type for fitting and prediction. O: The output prediction type.

Subclasses must implement the is_fitted property, fit method, predict method, and the state management methods from Stateful.

Example

Implementing a simple linear predictor

>>> class LinearPredictor(Predictor[list[float], float]):
...     def __init__(self):
...         self.slope = None
...         self.intercept = None
...
...     @property
...     def is_fitted(self) -> bool:
...         return self.slope is not None and self.intercept is not None
...
...     def fit(self, data: list[float]) -> None:
...         # Simple linear fit
...         self.slope = 1.0
...         self.intercept = 0.0
...
...     def predict(self, data: list[float]) -> float:
...         return self.slope * sum(data) + self.intercept
abstract property is_fitted: bool#

Check if the predictor has been fitted.

abstractmethod fit(data: I, data_val: I | None = None) Any[source]#

Fit the predictor to the input data.

This method should be called before generating predictions. It allows the predictor to learn parameters from the training data.

Parameters:
  • data (TypeVar(I)) – The training data to fit the predictor on.

  • data_val (Optional[TypeVar(I)]) – The validation data to evaluate and tune the predictor on (optional).

  • data

  • data_val

Return type:

Any

abstractmethod predict(data: I) O[source]#

Generate predictions for the input data.

This method should use the fitted parameters to generate predictions.

Parameters:
  • data (TypeVar(I)) – The input data to generate predictions for.

  • data

Returns:

Predictions for the input data.

Raises:

NotFittedError – If the predictor has not been fitted yet.

Return type:

TypeVar(O)

fit_predict(data: I, data_val: I | None = None) O[source]#

Fit the predictor to the data and then generate predictions.

This method combines fitting and prediction into a single step.

Parameters:
  • data (TypeVar(I)) – The input data to fit and generate predictions for.

  • data_val (Optional[TypeVar(I)]) – The validation data to evaluate and tune the predictor on (optional).

  • data

  • data_val

Returns:

Predictions for the input data.

Return type:

TypeVar(O)