Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-324

Lag Feature Matrix

Problem

Build a supervised learning design matrix from a series using p lagged values per row. Row t of the output is [x[t], x[t-1], ..., x[t-p+1]] for t from p-1 to n-1, so the first column is the current value and each later column is one more step back in time.

Return an empty list when p is not positive or p is larger than the series.

Hint

Each row consumes one more observation, so the matrix has n - p + 1 rows.

Test cases

case 1
in: [[1,2,3,4],2]
expected: [[2,1],[3,2],[4,3]]
case 2
in: [[1,2,3],1]
expected: [[1],[2],[3]]
case 3
in: [[1,2],3]
expected: []
case 4
in: [[],1]
expected: []
Starter codePython
def lag_feature_matrix(series, p):
    # Your code here
    pass

4 test cases · run your code against them in the editor