Lag Features
Lag Features
Lag features are created by shifting a time series backward in time, turning a sequential prediction problem into a tabular supervised learning problem. For each time step, lag features provide past observations as input features, allowing standard ML models (linear regression, random forests, etc.) to make forecasts.
Given a time series and a list of lag values, create a feature matrix where each row contains the values at the specified lags relative to that time step.
Algorithm
For each time step t (starting from the maximum lag), extract past values:
row(t)=[x[t−l1],x[t−l2],…,x[t−lk]]where l_1, l_2, ..., l_k are the specified lags. Only include time steps where all lags are available (t >= max(lags)).
Examples
Input:
series = [10, 20, 30, 40, 50], lags = [1, 2]
Output:
[[20, 10], [30, 20], [40, 30]]
Starting from t=2 (max lag): row = [series[1], series[0]] = [20, 10]. At t=3: [30, 20]. At t=4: [40, 30].
Input:
series = [1, 2, 3, 4, 5], lags = [1]
Output:
[[1], [2], [3], [4]]
With a single lag of 1, each row contains the previous value. Starting from t=1: series[0]=1, t=2: series[1]=2, etc.
Hint 1
Find max_lag = max(lags). Loop t from max_lag to len(series)-1. For each t, build a row: [series[t - lag] for lag in lags]. Append each row to the result.
Hint 2
The output has len(series) - max(lags) rows. Each row has len(lags) columns. Make sure you are looking backward (t - lag), not forward.
Requirements
- For each valid time step t, create a row with the values at each specified lag
- Only include rows where all lags are available (t >= max of lags)
- Preserve the order of lags as given in the input
- Return a list of lists
Constraints
- series has at least max(lags) + 1 elements
- lags is a non-empty list of positive integers
- Return a list of lists
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Lag Features
Lag Features
Lag features are created by shifting a time series backward in time, turning a sequential prediction problem into a tabular supervised learning problem. For each time step, lag features provide past observations as input features, allowing standard ML models (linear regression, random forests, etc.) to make forecasts.
Given a time series and a list of lag values, create a feature matrix where each row contains the values at the specified lags relative to that time step.
Algorithm
For each time step t (starting from the maximum lag), extract past values:
row(t)=[x[t−l1],x[t−l2],…,x[t−lk]]where l_1, l_2, ..., l_k are the specified lags. Only include time steps where all lags are available (t >= max(lags)).
Examples
Input:
series = [10, 20, 30, 40, 50], lags = [1, 2]
Output:
[[20, 10], [30, 20], [40, 30]]
Starting from t=2 (max lag): row = [series[1], series[0]] = [20, 10]. At t=3: [30, 20]. At t=4: [40, 30].
Input:
series = [1, 2, 3, 4, 5], lags = [1]
Output:
[[1], [2], [3], [4]]
With a single lag of 1, each row contains the previous value. Starting from t=1: series[0]=1, t=2: series[1]=2, etc.
Hint 1
Find max_lag = max(lags). Loop t from max_lag to len(series)-1. For each t, build a row: [series[t - lag] for lag in lags]. Append each row to the result.
Hint 2
The output has len(series) - max(lags) rows. Each row has len(lags) columns. Make sure you are looking backward (t - lag), not forward.
Requirements
- For each valid time step t, create a row with the values at each specified lag
- Only include rows where all lags are available (t >= max of lags)
- Preserve the order of lags as given in the input
- Return a list of lists
Constraints
- series has at least max(lags) + 1 elements
- lags is a non-empty list of positive integers
- Return a list of lists
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array