Hi good evening, I was wondering (thank you for th...
# mlforecast
b
Hi good evening, I was wondering (thank you for the support), if it possible to use this scaler in MLForecast.
Copy code
scaler = TemporalNorm(scaler_type='standard', dim=1)
I would like to implement a kind of window normalizer
j
What would the windows look like?
b
Not sure, 24 hours or a week, at the moment is just an idea based on these two papers
j
How would the predictions get restored? Would you use the last statistic?
b
I suppose I need to reflect on it, getting back when is clear on my side. Sorry to bother I will check this implementation: https://gist.github.com/markcutajar/be088ba2f3b6dd157f1037c24fceb3cd
j
This uses the last statistic to restore the predictions:
Copy code
import numpy as np
from coreforecast.grouped_array import GroupedArray as CoreGroupedArray
from mlforecast import MLForecast
from mlforecast.grouped_array import GroupedArray
from mlforecast.lag_transforms import RollingMean
from mlforecast.target_transforms import _BaseGroupedArrayTargetTransform
from sklearn.linear_model import LinearRegression
from utilsforecast.data import generate_series

class WindowStandardScaler(_BaseGroupedArrayTargetTransform):
    def __init__(self, window_size: int):
        self.window_size = window_size

    def update(self, ga: GroupedArray) -> GroupedArray:
        raise NotImplementedError

    def take(self, idxs: np.ndarray) -> 'WindowStandardScaler':
        raise NotImplementedError

    def update(self, ga: GroupedArray) -> GroupedArray:
        raise NotImplementedError

    def fit_transform(self, ga: GroupedArray) -> GroupedArray:
        rm = RollingMean(self.window_size, min_samples=1)
        rm._set_core_tfm(lag=0)
        core_ga = CoreGroupedArray(ga.data, ga.indptr)
        scales = rm.transform(core_ga)
        self.last_scales_ = CoreGroupedArray(scales, ga.indptr)._tail(1)
        return GroupedArray(ga.data / scales, ga.indptr)

    def inverse_transform(self, ga: GroupedArray) -> GroupedArray:
        h = ga.data.size // (ga.indptr.size - 1)
        scales = np.repeat(self.last_scales_, h)
        return GroupedArray(ga.data * scales, ga.indptr)

series = generate_series(10, min_length=100)
fcst = MLForecast(
    models=[LinearRegression()],
    freq="D",
    lags=[1, 2],
    target_transforms=[WindowStandardScaler(window_size=7)]
)
fcst.fit(series)
fcst.predict(5)
Feel free to experiment with it and let me know how it goes
I guess you could also save all the statistics and use the corresponding one to restore the prediction for each step, e.g. if h=10 you'd apply scales[-10] to rescale the first prediction, scales[-9] to restore the second, etc
Btw you can call
fcst.preprocess(series)
to explore what the scaled target looks like
b
Thank you very much for the support
I will put this in my current optuna pipeline