Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Lag features are a simple way to turn past values in a time series into input features for a model. You’ll build a lagged feature matrix from a 1D sequence so it can be fed into standard ML models.
Implement the function
Rules:
t = max(lags)).X and y as NumPy arrays.Output:
| Argument | Type |
|---|---|
| lags | list |
| series | np.ndarray |
| Return Name | Type |
|---|---|
| value | tuple |
Return NumPy arrays
Do not sort lags; preserve input order
Rows start at t = max(lags) only
Compute the first valid index t: you can only build a row when all t - lag indices are non-negative, so start at t = max(lags).
For each valid t in range(max_lag, n), build one row as [series[t - lag] for lag in lags] and append series[t] to y.
If using NumPy slicing for speed: for each lag L, the whole column is s[max_lag - L : n - L]; fill columns in the given lag order.