简介
本文以天气时间序列数据集为例,讨论如何使用深度学习模型预测未来天气温度。
使用的是由德国耶拿马克思普朗克生物地球化学研究所的气象站记录。
数据每10分钟记录一次,共记录14种不同的量,如气温、气压、湿度、风向等,原始数据可追溯到2003年,本例只使用2009-2016年的数据。
数据
下载并解压数据略。
加载数据:
data_dir = r'E:\practice\tf2\temperature'
fname = os.path.join(data_dir, 'jena_climate_2009_2016.csv')
f = open(fname)
data = f.read()
f.close()
lines = data.split('\n')
header = lines[0].split(',')
lines = lines[1:]
print(header)
print(len(lines))
如下图:

查看温度:
# 解析数据到Numpy向量
import numpy as np
float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):
values = [float(x) for x in line.split(',')[1:]]
float_data[i, :] = values
# 查看温度序列
from matplotlib import pyplot as plt
temp = float_data[:, 1]
plt.plot(range(len(temp)), temp)
plt.title('All days temperature')
plt.legend()
plt.figure()
# 全部的数据不容易看清楚,查看前10天的数据
plt.plot(range(1440), temp[:1440])
plt.title('First 10 days temperature')
plt.legend()
plt.show()
如下图:

准备数据
用数学语言重新描述一下问题:
一个时间步是10分钟,每个steps有一个数据点,给定过去lookback个时间步之内的数据,预测delay个时间步之后的温度。
假定:
- lookback = 720,即给定过去5天内的数据
- steps = 6,即采样频率1小时
- delay = 144,即预测未来24小时后的温度
开始数据预处理:
# 数据标准化
mean = float_data[:200000].mean(axis = 0)
float_data -= mean
std = float_data[:200000].std(axis = 0)
float_data /= std
由于数据集中样本高度冗余,编写一个数据生成器,使用原始数据即时生成样本:
# 生成器:生成时间序列样本及其目标
def generator(data, lookback, delay, min_index, max_index, shuffle = False, batch_size = 128, step = 6):
if max_index is None:
max_index = len(data) - delay - 1
i = min_index + lookback
while 1:
if shuffle
7523



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



