[numpy] Create a moving window matrix from multidimensional time series data

Former story

[\ numpy ] Create a moving window matrix from time series data --Qiita I tried to extend this to multidimensional data.

Thing you want to do

Given an ndarray x, considers the last dimension as a time and creates an ndarray y with the data contained in a moving window with a width of $ w $.

When x is one-dimensional

x = 
\begin{pmatrix}
x_0 & x_1 & \cdots & x_{n-1}
\end{pmatrix}

Then

y = 
\begin{pmatrix}
x_0 & x_1 & \cdots & x_{n-w} \\
x_1 & x_2 & \cdots & x_{n-w+1} \\
\vdots & \vdots & & \vdots \\
x_{w-1} & x_w & \cdots & x_{n-1} \\
\end{pmatrix}

It will be. Also, when x is two-dimensional

x = 
\begin{pmatrix}
x_{0,0} & x_{0,1} & \cdots & x_{0,n-1} \\
\vdots & \vdots & & \vdots \\
x_{m-1,0} & x_{m-1,1} & \cdots & x_{m-1,n-1}
\end{pmatrix}

Then, when slicing in the third dimension

y_{:,:,t} = 
\begin{pmatrix}
x_{0,t} & x_{0,t+1} & \cdots & x_{0,t+w-1} \\
\vdots & \vdots & & \vdots \\
x_{m-1,t} & x_{m-1,t+1} & \cdots & x_{m-1,t+w-1}
\end{pmatrix}

Create an ndarray y such that It is the same even if there are more dimensions.

code

import numpy as np

def moving_window_matrix(x, window_size):
    n = x.shape[-1]
    new_shape = tuple(x.shape[0:-1]) + (window_size, n-window_size+1)
    stride = x.strides[-1]
    return np.lib.stride_tricks.as_strided(
        x,
        shape=new_shape,
        strides=x.strides + (stride,)).copy()

x = np.arange(30).reshape((3,10))
print(x)
window_size = 3
y = moving_window_matrix(x, window_size)
print(y[:, :, 0])
print(y[:, :, 1])
print(y[:, :, 2])
print(y[:, :, 7])

Recommended Posts

[numpy] Create a moving window matrix from multidimensional time series data
Get time series data from k-db.com in Python
Extract periods that match a particular pattern from pandas time series qualitative data
Features that can be extracted from time series data
Predict from various data in Python using Facebook Prophet, a time series prediction tool
Create a data frame from the acquired boat race text data
Library tsfresh that automatically extracts features from time series data
A story about clustering time series data of foreign exchange
A Python program that aggregates time usage from icalendar data
Create a python numpy array
Create a dummy data file
[Python] Plot time series data
Create a summary table by product and time by processing the data extracted from a certain POS system
Create an API that returns data from a model using turicreate
<Pandas> How to handle time series data in a pivot table
When plotting time series data and getting a matplotlib Overflow Error
Python: Time Series Analysis: Preprocessing Time Series Data
Create a pandas Dataframe from a string.
About time series data and overfitting
Differentiation of time series data (discrete)
Time series analysis 3 Preprocessing of time series data
July, a certain, M5 ~ Kaggle beginner time series data competition failure story ~
Create multiple line charts from a data frame at once using Matplotlib
[Kaggle] I tried feature engineering of multidimensional time series data using tsfresh.