import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow.keras
import warnings
from sklearn import preprocessing
warnings.filterwarnings("ignore")
%matplotlib inline
1.数据预处理
1.1导入数据
temperatures = pd.read_csv('tf_temperature.csv')
print("数据维度", temperatures.shape)
temperatures.head()
数据维度 (348, 9)
year | month | day | week | temp_2 | temp_1 | average | actual | friend | |
---|---|---|---|---|---|---|---|---|---|
0 | 2016 | 1 | 1 | Fri | 45 | 45 | 45.6 | 45 | 29 |
1 | 2016 | 1 | 2 | Sat | 44 | 45 | 45.7 | 44 | 61 |
2 | 2016 | 1 | 3 | Sun | 45 | 44 | 45.8 | 41 | 56 |
3 | 2016 | 1 | 4 | Mon | 44 | 41 | 45.9 | 40 | 53 |
4 | 2016 | 1 | 5 | Tues | 41 | 40 | 46.0 | 44 | 41 |
数据说明
- temp_2 前天的最高温度值
- temp_1 昨天的最高温度值
- averate 历史上,每年这一天的平均最高温度值
- actual 标签值,当天的真实的最高温度值
- friend 朋友猜测的温度值,忽略
1.2 处理时间数据
years = temperatures['year']
months = temperatures['month']
days = temperatures['day']
#转为datetime格式
temp_days = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in zip(years, months, days)]
temp_days = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in temp_days]
print(temp_days[:5])
[datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2016, 1, 2, 0, 0), datetime.datetime(2016, 1, 3, 0, 0), datetime.datetime(2016, 1, 4, 0, 0), datetime.datetime(2016, 1, 5, 0, 0)]
1.3 原始数据画图展示
#画图风格
plt.style.use('fivethirtyeight')
#布局
fig,((plt1,plt2),(plt3,plt4)) = plt.subplots(nrows=2, ncols=2,figsize=(10,10))
fig.autofmt_xdate(rotation = 45)
#标签值
plt1.plot(temp_days, temperatures['actual'])
plt1.set_xlabel('')
plt1.set_ylabel('Temperature')
plt1.set_title('Max Temp')
plt2.plot(temp_days, temperatures['temp_1'])
plt2.set_xlabel('')
plt2.set_ylabel('Temperature')
plt2.set_title('Yesterday Max Temp')
plt3.plot(temp_days, temperatures['temp_2'])
plt3.set_xlabel('')
plt3.set_ylabel('Temperature')
plt3.set_title('The Day Before Yesterday Temp')
plt4.plot(temp_days, temperatures['friend'])
plt4.set_xlabel('')
plt4.set_ylabel(