Zac*_*ach 5 python machine-learning neural-network keras recurrent-neural-network
我正在尝试使用python/keras构建一个RNN.我理解如何使用一个功能(t + 1作为输出),但是如何使用多个功能?
如果我有一个回归问题和一个具有一些不同特征的数据集,一个预期输出,并且我希望将时间步长/窗口设置为30(如果每个步骤代表一天,那么一个月)怎么办?数据是?在这个例子中,我希望能够预测未来的输出n个时间段.
请参阅下面的示例,了解此数据的外观:
我很难直观地了解RNN数据所需的最佳形状/格式.
此外,RNN如何处理数据集,例如500个功能和几千条记录?
希望有人可以帮助回答或指出我正确的方向得到一个 - 到目前为止,我已发布在Reddit和Cross验证没有运气:(
如果首选代码数据示例:
# random df
df = pd.DataFrame({'date': np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
'feature_1': np.random.randint(10, size=10),
'feature_2': np.random.randint(10, size=10),
'feature_3': np.random.randint(10, size=10),
'feature_4': np.random.randint(10, size=10),
'output': np.random.randint(10, size=10)}
)
# set date as index
df.index = df.date
df = df.drop('date', 1)
Run Code Online (Sandbox Code Playgroud)
假设您有 2 个时间序列 X 和 Y,并且您想使用这两个时间序列来预测 X。如果我们选择时间步长 3 并假设我们有 和 ,(X1,...,Xt)则(Y1,...,Yt)第一个样本将是 :
[[X1,X2,X3],[Y1,Y2,Y3]]以及相关的输出 :X4。第二个将[[X2,X3,X4],[Y2,Y3,Y4]]作为X5输出。最后一个:[[Xt-3,Xt-2,Xt-1],[Yt-3,Yt-2,Yt-1]]作为Xt输出。
例如,在第一个示例中:首先您将向网络提供(X1,Y1),然后(X2,Y2)和(X3,Y3)。
下面是创建输入和输出,然后使用 LSTM 网络进行预测的代码:
import pandas as pd
import numpy as np
import keras.optimizers
from keras.models import Sequential
from keras.layers import Dense,Activation
from keras.layers import LSTM
#random df
df = pd.DataFrame({'date': np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
'feature_1': np.random.randint(10, size=10),
'feature_2': np.random.randint(10, size=10),
'feature_3': np.random.randint(10, size=10),
'feature_4': np.random.randint(10, size=10),
'output': np.random.randint(10, size=10)}
)
# set date as index
df.index = df.date
df = df.drop('date', 1)
nb_epoch = 10
batch_size = 10
learning_rate = 0.01
nb_units = 50
timeStep = 3
X = df[['feature_'+str(i) for i in range(1,5)]].values # Select good columns
sizeX = X.shape[0]-X.shape[0]%timeStep # Choose a number of observations that is a multiple of the timstep
X = X[:sizeX]
X = X.reshape(X.shape[0]/timeStep,timeStep,X.shape[1]) # Create X with shape (nb_sample,timestep,nb_features)
Y = df[['output']].values
Y = Y[range(3,len(Y),3)] #Select the good output
model = Sequential()
model.add(LSTM(input_dim = X.shape[2],output_dim = nb_units,return_sequences = False)) # One LSTM layer with 50 units
model.add(Activation("sigmoid"))
model.add(Dense(1)) #A dense layer which is the final layer
model.add(Activation('linear'))
KerasOptimizer = keras.optimizers.RMSprop(lr=learning_rate, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(loss="mse", optimizer=KerasOptimizer)
model.fit(X,Y,nb_epoch = nb_epoch,batch_size = batch_size)
prediction = model.predict(X)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1432 次 |
| 最近记录: |