import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.optim as optim
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
搭建Pytorch神经网路进行气温预测
获取数据并,展示分析数据
features = pd.read_csv('temps.csv')
features.head()
|
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:昨天的最高气温
- average: 每年这一天的平均最高温度值
- actual : 实际的值,y值,要预测的值
- friend : 一个不准确的数据,朋友猜的今天的气温
features.shape
(348, 9)
import datetime
years = features['year']
months = features['month']
days = features['day']
dates = [str(int(year))+'-'+str(int(month))+'-'+str(int(day)) for year,month,day in zip(years,months,days)]
dates = [datetime.datetime.strptime(date,'%Y-%m-%d') for date in dates]
dates[: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)]
plt.style.use('fivethirtyeight')
fig,((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows=2,ncols=2,figsize=(10,10))
fig.autofmt_xdate(rotation=45)
ax1.plot(dates,features['actual'])
ax1.set_xlabel('');ax1.set_ylabel('Temperature');ax1.set_title('Max Temp')
ax2.plot(dates,features['temp_1'])
ax2.set_xlabel('');ax2.set_ylabel('Temperature');ax2.set_title('Previous Max Temp')
ax3.plot(dates,features['temp_2'])
ax3.set_xlabel('');ax3.set_ylabel('Temperature')