试图建立简单的LSTM模型来预测未来24小时的用电量。当前模型可以预测提前1步(1小时),并且希望对其进行改进以预测未来24步。
添加了更多代码以查看数据形状。试图通过此post的示例来找到解决方案,但未成功。 CSV文件有两列,1-日期,2-每小时消耗量。
# define a function to convert a vector of time series into a 2D matrix
def convertSeriesToMatrix(vectorSeries, sequence_length):
matrix=[]
for i in range(len(vectorSeries)-sequence_length+1):
matrix.append(vectorSeries[i:i+sequence_length])
return matrix
# random seed
np.random.seed(1234)
# load raw data
df_raw = pd.read_csv(r'C:\Users\vytlksn\Documents\Python_C\Electric-Power- Hourly-Load-Forecasting-using-Recurrent-Neural-Networks-master\Sadales data comma 3.csv''', header=None)
# numpy array
df_raw_array = df_raw.values
# daily load
list_daily_load = [df_raw_array[i,:] for i in range(0, len(df_raw)) if i % 24 == 0]
# hourly load (23 loads for each day)
list_hourly_load = [df_raw_array[i,1]/1 for i in range(0, len(df_raw)) if i % 24 != 0]
# the length of the sequnce for predicting the future value
sequence_length = 24
# convert the vector to a 2D matrix
matrix_load = convertSeriesToMatrix(list_hourly_load, sequence_length)
# shift all data by mean
matrix_load = np.array(matrix_load)
shifted_value = matrix_load.mean()
matrix_load -= shifted_value
print ("Data shape: ", matrix_load.shape)
# split dataset: 90% for training and 10% for testing
train_row = int(round(0.8 * matrix_load.shape[0]))
train_set = matrix_load[:train_row, :]
# shuffle the training set (but do not shuffle the test set)
np.random.shuffle(train_set)
# the training set
X_train = train_set[:, :-1]
# the last column is the true value to compute the mean-squared-error loss
y_train = train_set[:, -1]
# the test set
X_test = matrix_load[train_row:, :-1]
y_test = matrix_load[train_row:, -1]
# the input to LSTM layer needs to have the shape of (number of samples, the dimension of each element)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
# build the model
model = Sequential()
# layer 1: LSTM
model.add(LSTM( input_dim=1, output_dim=50, return_sequences=True))
model.add(Dropout(0.2))
# layer 2: LSTM
model.add(LSTM(output_dim=100, return_sequences=False))
model.add(Dropout(0.2))
# layer 3: dense
# linear activation: a(x) = x
model.add(Dense(output_dim=1, activation='linear'))
# compile the model
model.compile(loss="mse", optimizer="rmsprop")
# train the model
model.fit(X_train, y_train, batch_size=24, nb_epoch=25, validation_split=0.05, verbose=1)
# evaluate the result
test_mse = model.evaluate(X_test, y_test, verbose=1)
print ('\nThe mean squared error (MSE) on the test data set is %.3f over %d test samples.' % (test_mse, len(y_test)))
# get the predicted values
predicted_values = model.predict(X_test)
num_test_samples = len(predicted_values)
predicted_values = np.reshape(predicted_values, (num_test_samples,1))