Repository navigation
How to compute rolling weighted moving average as feature during training and prediction. #339
Unanswered
Anand Prakash (anand086)
asked this question in
Q&A
Replies: 1 comment
|
Hey. This isn't really straightforward, but you can achieve it with the following: import numpy as np
import pandas as pd
from mlforecast import MLForecast
from mlforecast.feature_engineering import transform_exog
from sklearn.linear_model import LinearRegression
from utilsforecast.data import generate_series
horizon = 10
window_size = 7
# create target
series = generate_series(1, min_length=100, max_length=100)
# create weights. note that you'll need the future values of these
weights = pd.DataFrame(
{
'unique_id': series.loc[0, 'unique_id'],
'ds': pd.date_range(series['ds'].min(), series['ds'].max() + horizon * pd.offsets.Day()),
'weight': np.random.rand(series.shape[0] + horizon)
}
)
# assign the weight for each lag
lags = range(1, window_size + 1)
full_weights = transform_exog(weights, lags=lags).dropna().drop(columns='weight')
# merge to series. this drops the first observations because there aren't weights for that lag
train = series.merge(full_weights, on=['unique_id', 'ds'], how='inner')
fcst = MLForecast(
models=[LinearRegression()],
lags=lags,
freq='D',
)
# create the lags
X, y = fcst.preprocess(train, static_features=[], return_X_y=True)
# define a function that will compute the weighted rolling mean by multiplying the lags and weights columns
def compute_features(df):
df = df.copy(deep=False)
lag_cols = [f'lag{i+1}' for i in range(7)]
weight_cols = [f'weight_lag{i+1}' for i in range(7)]
lags = df[lag_cols].to_numpy()
weights = df[weight_cols].to_numpy()
df['rolling_wma_7'] = (lags * weights).sum(axis=1) / weights.sum(axis=1)
df = df.drop(columns=lag_cols + weight_cols)
return df
# apply this processing to the training set
prep_X = compute_features(X)
# train the models on that
fcst.fit_models(prep_X, y)
# provide the future weights and the processing function
fcst.predict(horizon, X_df=full_weights, before_predict_callback=compute_features) |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
First of all, thank you for creating this wonderful library. I am working on a time-series problem to forecast for upcoming 21 days and was wondering how can I implement rolling weighted moving average. During feature engineering I have used the below code to calculate rolling weighted moving average, but I am not sure how can I provide during predict. My understanding is that when I go to make forecasts recursively, then I would need to compute the value of this features after making my predictions for each period.
All reactions