写一段代码使用LSTM根据天气数据对风电场输出功率进行预测
时间: 2024-05-02 12:19:51 浏览: 104
LSTM天气预测数据集
好的,以下是使用LSTM进行风电场输出功率预测的代码:
```python
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
# 读入天气数据和风电场输出功率数据
weather_data = pd.read_csv('weather.csv')
power_data = pd.read_csv('power.csv')
# 合并数据
data = pd.concat([weather_data, power_data], axis=1)
# 数据预处理
scaler = MinMaxScaler(feature_range=(0, 1))
data_scaled = scaler.fit_transform(data)
# 划分训练集和测试集
train_size = int(len(data_scaled) * 0.7)
test_size = len(data_scaled) - train_size
train_data = data_scaled[0:train_size,:]
test_data = data_scaled[train_size:len(data_scaled),:]
# 构造输入和输出数据
def create_dataset(data, time_step=1):
X, y = [], []
for i in range(len(data)-time_step-1):
X.append(data[i:(i+time_step), 0])
y.append(data[i+time_step, 1])
return np.array(X), np.array(y)
time_step = 30
X_train, y_train = create_dataset(train_data, time_step)
X_test, y_test = create_dataset(test_data, time_step)
# 调整输入数据形状
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
# 构造模型
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(units=50))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()
# 训练模型
model.fit(X_train, y_train, epochs=100, batch_size=32)
# 在测试集上进行预测
y_pred = model.predict(X_test)
# 反归一化预测数据
y_pred = scaler.inverse_transform(y_pred)
# 计算预测误差
rmse = np.sqrt(np.mean(((y_pred - y_test)**2)))
print('Root Mean Square Error:', rmse)
```
希望这些代码能够帮到你!如果你有更多问题,请随时问我。
阅读全文