Predictor#
- class openstef_core.mixins.predictor.Predictor[source]
-
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.
- 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)