写一个lstm预测风电功率的代码
时间: 2023-04-04 16:03:45 浏览: 135
基于卷积长短期神经网络的风电功率预测,基于cnn-lstm的风电功率预测,基于LSTM的风电功率预测(代码完整,数据齐全)
以下是一个简单的LSTM模型,用于预测风电功率:
```python
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, LSTM
# 加载数据
data = pd.read_csv('wind_power.csv')
data = data.dropna()
# 准备数据
X = data[['wind_speed', 'wind_direction']].values
y = data['power'].values
# 归一化数据
X = (X - np.mean(X)) / np.std(X)
y = (y - np.mean(y)) / np.std(y)
# 划分训练集和测试集
train_size = int(len(X) * 0.8)
X_train, y_train = X[:train_size], y[:train_size]
X_test, y_test = X[train_size:], y[train_size:]
# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(X_train.shape[1], 1)))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# 训练模型
model.fit(X_train.reshape((X_train.shape[0], X_train.shape[1], 1)), y_train, epochs=50, batch_size=32, verbose=2)
# 测试模型
y_pred = model.predict(X_test.reshape((X_test.shape[0], X_test.shape[1], 1)))
mse = np.mean((y_pred - y_test) ** 2)
print('MSE:', mse)
```
请注意,此代码仅供参考,您需要根据您的数据和需求进行修改和调整。
阅读全文