[英]Train machine learning model with scikit learn for time-series prediction
[英]Efficient way to Reshape Data for Time Series Prediction Machine Learning (Numpy)
可以說我有一個N個時間序列樣本的數據集(numpy數組)X,每個樣本具有D維向量的T個時間步長,因此:
X.shape == (N,T,D)
現在,我想將其重塑為x(數據集)和y(標簽),以應用機器學習來預測時間序列中的步驟。
我想取長度為n的每個樣本的每個子系列
x.shape==(N*(T-n),n,D) and y.shape==(N*(T-n)),D)
與
X[k,j:j+n,:]
是我在x
和
X[k,j+n+1,:]
它在y
標記。
for-loop是這樣做的唯一方法嗎?
所以我有以下方法,但是它有一個for循環,而且我不確定我不能做得更好:
def reshape_data(self, X, n):
"""
Reshape a data set of N time series samples of T time steps each
Args:
data: Time series data of shape (N,T,D)
n: int, length of time window used to predict x[t+1]
Returns:
"""
N,T,D = X.shape
x = np.zeros((N*(T-n),n,D))
y = np.zeros((N*(T-n),D))
for i in range(T-n):
x[N*i:N*(i+1),:,:] = X[:,i:i+n,:]
y[N*i:N*(i+1),:] = X[:,i+n,:]
return x,y
您正在尋找pandas data panel
。 ( http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.html )。 只需放入numpy數組中,在短軸上轉置並獲取其numpy表示形式( .as_matrix()
或簡稱為.values
)。 如果您只想僅在numpy中真正做到這一點, numpy.transpose
僅用於( https://docs.scipy.org/doc/numpy/reference/generation/numpy.transpose.html )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.