Predictor#
- class openstef_core.mixins.Predictor[I, O][source]#
Bases:
Stateful,Generic[I,O]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
- abstractmethod fit(data: I, data_val: Optional[I] = 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 (
I) – The input data to generate predictions for.- Return type:
O- Returns:
Predictions for the input data.
- Raises:
NotFittedError – If the predictor has not been fitted yet.
- fit_predict(data: I, data_val: Optional[I] = 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 (
I) – The input data to fit and generate predictions for.data_val (
Optional[I]) – The validation data to evaluate and tune the predictor on (optional).
- Return type:
O- Returns:
Predictions for the input data.