0 简介
今天学长向大家介绍一个机器视觉的毕设项目
使用LSTM实现天气时间序列预测
项目运行效果:
毕业设计 lstm天气预测
🧿 项目分享:见文末!
1. 数据集介绍
数据集包含14个不同的特征,例如气温,大气压力和湿度。从2003年开始,每10分钟收集一次。为了提高效率,本文仅使用2009年至2016年之间收集的数据。
** 加载数据集**

如上所示,每10分钟记录一次观测值,一个小时内有6个观测值,一天有144(6x24)个观测值。
给定一个特定的时间,假设要预测未来6小时的温度。为了做出此预测,选择使用5天的观察时间。因此,创建一个包含最后720(5x144)个观测值的窗口以训练模型。
下面的函数返回上述时间窗以供模型训练。参数 history_size 是过去信息的滑动窗口大小。target_size
是模型需要学习预测的未来时间步,也作为需要被预测的标签。
下面使用数据的前300,000行当做训练数据集,其余的作为验证数据集。总计约2100天的训练数据。
2. 单变量单步输出预测
首先,使用一个特征(温度)训练模型,并在使用该模型做预测。
1.从数据集中提取温度
uni_data = df['T (degC)']
uni_data.index = df['Date Time']
uni_data.head()
2.观察一下这些数据随时间变化的情况
uni_data.plot(subplots=True)

2.1 数据标准化
在训练神经网络之前缩放特征很重要。标准化是通过减去平均值并除以每个特征的标准偏差来进行缩放的一种常用方法。也可以使用
tf.keras.utils.normalize 将值缩放到[0,1]范围内。
uni_data = uni_data.values
uni_train_mean = uni_data[:TRAIN_SPLIT].mean()
uni_train_std = uni_data[:TRAIN_SPLIT].std()
uni_data = (uni_data-uni_train_mean)/uni_train_std
2.2 样本划分
现在为单变量模型创建数据。本部分为模型提供最后20个温度观测值,预测下一个时间步长的温度。
univariate_past_history = 20
univariate_future_target = 0
# shape 分别为:(299980, 20, 1);(299980,)
x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,
univariate_past_history,
univariate_future_target)
x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT, None,
univariate_past_history,
univariate_future_target)
输出:
Single window of past history
[[-1.99766294]
[-2.04281897]
[-2.05439744]
[-2.0312405 ]
[-2.02660912]
[-2.00113649]
[-1.95134907]
[-1.95134907]
[-1.98492663]
[-2.04513467]
[-2.08334362]
[-2.09723778]
[-2.09376424]
[-2.09144854]
[-2.07176515]
[-2.07176515]
[-2.07639653]
[-2.08913285]
[-2.09260639]
[-2.10418486]]
Target temperature to predict
-2.1041848598100876
2.3 绘制曲线
现在已经创建了数据,看一个例子。提供给网络的信息以蓝色表示,并且它必须预测红叉处的值。
def create_time_steps(length):
return list(range(-length, 0))
def show_plot(plot_data, delta, title):
labels = ['History', 'True Future', 'Model Prediction']
marker = ['.-', 'rx', 'go']
time_steps = create_time_steps(plot_data[0].shape[0]) # 横轴刻度
if delta:
future = delta
else:
future = 0
plt.title(title)
for i, x in enumerate(plot_data):
if i:
plt.plot(future, plot_data[i], marker[i], markersize=10,
label=labels[i])
else:
plt.plot(time_steps, plot_data[i].flatten(), marker[i], label=labels[i])
plt.legend()
plt.xlim([time_steps[0]

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



