Create a forecast matrix from timeserie samples

Viewed 27

I would like to create a matrix of delay from a timeserie.

For example if

y = [y_0, y_1, y_2, ..., y_N] and W = 5

I would like to create the matrix

| 0       | 0       | 0       | 0       | 0   |
| 0       | 0       | 0       | 0       | y_0 |
| 0       | 0       | 0       | y_0     | y_1 |
| ...     |         |         |         |     |
| y_{N-4} | y_{N-3} | y_{N-2} | y_{N-1} | y_N |

I know that function timeseries_dataset_from_array from tensorflow do approximatively the same thing when well configured but I would like to avoid using tensorflow.

This is my current function to perform this task:

def get_warm_up_matrix(_data: ndarray, W: int) -> ndarray:
    """
    Return a warm-up matrix
    If _data = [y_1, y_2, ..., y_N]
    The output matrix W will be
    W =     +---------+-----+---------+---------+-----+
            | 0       | ... | 0       | 0       | 0   |
            | 0       | ... | 0       | 0       | y_1 |
            | 0       | ... | 0       | y_1     | y_2 |
            | ...     | ... | ...     | ...     | ... |
            | y_1     | ... | y_{W-2} | y_{W-1} | y_W |
            | ...     | ... | ...     | ...     | ... |
            | y_{N-W} | ... | y_{N-2} | y_{N-1} | y_N |
            +---------+-----+---------+---------+-----+
    :param _data:
    :param W:
    :return:
    """
    N = len(_data)

    warm_up = np.zeros((N, W), dtype=_data.dtype)
    raw_data_with_zeros = np.concatenate((np.zeros(W, dtype=_data.dtype), _data), dtype=_data.dtype)

    for k in range(W, N + W):
        warm_up[k - W, :] = raw_data_with_zeros[k - W:k]

    return warm_up

It works well, but it's quite slow since the concatenate operation and the for loop take time to be performed. It also take a lot of memory since the data have to be duplicated in memory before filling the matrix.

I would like a faster and memory-friendly method to perform the same task. Thanks for your help :)

0 Answers
Related