multitask.strategies.LazyStrategy(use_tuned_params= True , max_age_days= None )
Approach 1 — Lazy fitting with optional cached tuning.
Mirrors the body of execute_lazy between create_forecaster() and _train_and_predict_target().
Examples
import pandas as pd
from sklearn.linear_model import LinearRegression
from spotforecast2_safe.forecaster.recursive import ForecasterRecursive
from spotforecast2_safe.multitask.strategies import LazyStrategy
# Construct with default settings (cache lookup enabled).
strategy = LazyStrategy(use_tuned_params= True , max_age_days= 30.0 )
assert strategy.name == "lazy"
assert strategy.use_tuned_params is True
assert strategy.max_age_days == 30.0
print (f"strategy.name= { strategy. name!r} , use_tuned_params= { strategy. use_tuned_params} " )
# When no cached results exist, the forecaster is returned unchanged.
class _NullTask:
class logger:
@staticmethod
def info(* a, ** kw):
pass
def load_tuning_results(self , target, max_age_days= None ):
return None
forecaster = ForecasterRecursive(estimator= LinearRegression(), lags= 3 )
y_train = pd.Series(range (30 ), dtype= float , name= "target_0" )
result = strategy.prepare_forecaster(_NullTask(), "target_0" , forecaster, y_train)
assert result is forecaster
print (f"Forecaster returned unchanged when cache miss: { result is forecaster} " )
strategy.name='lazy', use_tuned_params=True
Forecaster returned unchanged when cache miss: True
Methods
prepare_forecaster
multitask.strategies.LazyStrategy.prepare_forecaster(
task,
target,
forecaster,
y_train,
exog_train= None ,
)
Optionally apply cached tuning results and return the forecaster.
When use_tuned_params is False, or when no cached results are found for target, the forecaster is returned without modification. Otherwise, best_params are applied via forecaster.set_params and best_lags via forecaster.set_lags (when available).
Parameters
task
Any
A BaseTask instance that exposes load_tuning_results and a logger.
required
target
str
The target name used as key when loading cached results.
required
forecaster
Any
The unfitted forecaster returned by the factory.
required
y_train
pd .Series
Training series (unused by this strategy; kept for protocol compatibility).
required
exog_train
Optional [pd .DataFrame ]
Exogenous training frame (unused by this strategy).
None
Returns
Any
The same forecaster object, with parameters updated in-place when
Any
cached tuning results were found and applied.
Examples
import pandas as pd
from sklearn.linear_model import LinearRegression
from spotforecast2_safe.forecaster.recursive import ForecasterRecursive
from spotforecast2_safe.multitask.strategies import LazyStrategy
forecaster = ForecasterRecursive(estimator= LinearRegression(), lags= 4 )
y_train = pd.Series(range (30 ), dtype= float , name= "target_0" )
# --- Path 1: use_tuned_params=False — forecaster returned as-is ---
strategy_no_cache = LazyStrategy(use_tuned_params= False )
class _NullTask:
class logger:
@staticmethod
def info(* a, ** kw):
pass
def load_tuning_results(self , target, max_age_days= None ):
return None
result = strategy_no_cache.prepare_forecaster(
_NullTask(), "target_0" , forecaster, y_train
)
assert result is forecaster
print (f"Path 1 (no cache): lags unchanged = { list (result.lags)} " )
# --- Path 2: cached results present — lags and params applied ---
class _CachedTask:
class logger:
@staticmethod
def info(* a, ** kw):
pass
def load_tuning_results(self , target, max_age_days= None ):
return {
"task_name" : "lazy" ,
"timestamp" : "2026-01-01T00:00:00" ,
"best_params" : {},
"best_lags" : [1 , 2 , 3 ],
}
strategy_with_cache = LazyStrategy(use_tuned_params= True )
result2 = strategy_with_cache.prepare_forecaster(
_CachedTask(), "target_0" , forecaster, y_train
)
assert list (result2.lags) == [1 , 2 , 3 ]
print (f"Path 2 (cache hit): lags updated to { list (result2.lags)} " )
Path 1 (no cache): lags unchanged = [np.int64(1), np.int64(2), np.int64(3), np.int64(4)]
Path 2 (cache hit): lags updated to [np.int64(1), np.int64(2), np.int64(3)]