Skip to content

Commit

Permalink
[pre-commit.ci] pre-commit autoupdate (#6994)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
pre-commit-ci[bot] committed Aug 1, 2023
1 parent 4d8ac26 commit af22e0a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.23.2
rev: 0.23.3
hooks:
- id: check-github-workflows

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.276
rev: v0.0.281
hooks:
- id: ruff
args: ["--fix"]
Expand Down

1 comment on commit af22e0a

@littlehorseman
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

1. 数据加载和预处理

data = pd.read_csv('data.csv')
data['时间'] = pd.to_datetime(data['时间']) # 将时间列转换为日期格式
data.set_index('时间', inplace=True) # 将时间列设置为索引
data = data.dropna() # 去除缺失值

2. 特征归一化

scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(data)

3. 创建训练集和测试集

假设你有50条数据,可以将前40条作为训练集,后10条作为测试集

train_size = 40
train_data = scaled_data[:train_size, :]
test_data = scaled_data[train_size:, :]

4. 定义函数生成训练数据和标签

def create_dataset(dataset, time_steps=1):
X, Y = [], []
for i in range(len(dataset) - time_steps):
a = dataset[i:(i + time_steps), :]
X.append(a)
Y.append(dataset[i + time_steps, 5]) # 灌溉量是第六列数据,索引为5
return np.array(X), np.array(Y)

time_steps = 5 # 设定LSTM的时间步长,这里设为5
X_train, y_train = create_dataset(train_data, time_steps)
X_test, y_test = create_dataset(test_data, time_steps)

5. 构建LSTM模型

model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(time_steps, 5)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(1))

6. 编译模型

model.compile(optimizer='adam', loss='mean_squared_error')

7. 训练模型

model.fit(X_train, y_train, epochs=100, batch_size=16, verbose=2)

8. 预测并还原数据

train_predict = model.predict(X_train)
test_predict = model.predict(X_test)

train_predict = scaler.inverse_transform(train_predict)
y_train = scaler.inverse_transform([y_train])
test_predict = scaler.inverse_transform(test_predict)
y_test = scaler.inverse_transform([y_test])

9. 可视化结果

import matplotlib.pyplot as plt

plt.plot(np.arange(0, len(y_train[0])), y_train[0], label="True Value")
plt.plot(np.arange(len(y_train[0]), len(y_train[0]) + len(y_test[0])), y_test[0], label="True Value(Test)")
plt.plot(np.arange(len(y_train[0])), train_predict[:, 0], label="Prediction")
plt.plot(np.arange(len(y_train[0]), len(y_train[0]) + len(y_test[0])), test_predict[:, 0], label="Prediction(Test)")
plt.legend()
plt.show()

Please sign in to comment.