TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Lag Features

Time SeriesFeature Engineering
Easy

Lag features convert a time series into rows that a supervised learning model can use. Each row represents one valid time step and contains earlier observations selected by the requested lags.

For a time step t and lags l_1 through l_k, construct

row⁡(t)=[xt−l1,xt−l2,…,xt−lk]\operatorname{row}(t) = [x_{t-l_1}, x_{t-l_2}, \ldots, x_{t-l_k}]row(t)=[xt−l1​​,xt−l2​​,…,xt−lk​​]

Here, x_t is the value at time t and l_j is the j-th requested lag. Begin at the largest lag so every referenced observation exists. Return the feature matrix as a list of lists, preserving the supplied lag order.

Loading visualization...

Examples

Input: series = [10, 20, 30, 40, 50], lags = [1, 2]

Output: [[20, 10], [30, 20], [40, 30]]

Explanation: At time 2, lag 1 selects 20 and lag 2 selects 10. The same lookup is repeated for each later time.

Input: series = [1, 2, 3, 4, 5], lags = [1]

Output: [[1], [2], [3], [4]]

Hint 1

Start the outer loop at max(lags).

Hint 2

Build each row with series[t - lag] for lags in their given order.

Requirements

  • For every valid time step, collect the values at the requested lags.
  • Begin at the maximum lag so every lookup is in bounds.
  • Preserve the order of lags within each row.
  • Return a list of lists.

Constraints

  • series contains at least max(lags) + 1 values.
  • lags is a nonempty list of positive integers.
  • Every lag is smaller than len(series).
  • Time limit: 300 ms.
Try Similar Problems
DifferencingExponential Moving AverageSimple Moving AverageAutocorrelationCumulative Returns

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Lag Features

Time SeriesFeature Engineering
Easy

Lag features convert a time series into rows that a supervised learning model can use. Each row represents one valid time step and contains earlier observations selected by the requested lags.

For a time step t and lags l_1 through l_k, construct

row⁡(t)=[xt−l1,xt−l2,…,xt−lk]\operatorname{row}(t) = [x_{t-l_1}, x_{t-l_2}, \ldots, x_{t-l_k}]row(t)=[xt−l1​​,xt−l2​​,…,xt−lk​​]

Here, x_t is the value at time t and l_j is the j-th requested lag. Begin at the largest lag so every referenced observation exists. Return the feature matrix as a list of lists, preserving the supplied lag order.

Loading visualization...

Examples

Input: series = [10, 20, 30, 40, 50], lags = [1, 2]

Output: [[20, 10], [30, 20], [40, 30]]

Explanation: At time 2, lag 1 selects 20 and lag 2 selects 10. The same lookup is repeated for each later time.

Input: series = [1, 2, 3, 4, 5], lags = [1]

Output: [[1], [2], [3], [4]]

Hint 1

Start the outer loop at max(lags).

Hint 2

Build each row with series[t - lag] for lags in their given order.

Requirements

  • For every valid time step, collect the values at the requested lags.
  • Begin at the maximum lag so every lookup is in bounds.
  • Preserve the order of lags within each row.
  • Return a list of lists.

Constraints

  • series contains at least max(lags) + 1 values.
  • lags is a nonempty list of positive integers.
  • Every lag is smaller than len(series).
  • Time limit: 300 ms.
Try Similar Problems
DifferencingExponential Moving AverageSimple Moving AverageAutocorrelationCumulative Returns

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.