# 搭建神经网络进行气温预测

### 回归问题预测

• Tensordlow2版本中将大量使用keras的简介建模方法
``````import numpy as np
import pandas as pd
import marplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow.keras
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline
``````
``````features = pd.read_csv('temps.csv')

# 看看数据长什么样子
``````

• year,month,day,week 分别表示具体的时间
• temp_2：前天的最高气温
• temp_1：昨天的最高气温
• average：在历史中，每年这一天的平均最高温度值
• actual：这就是我们的标签值了，当天的真实最高温度
• friend：这一列可能是凑热闹的，你的朋友猜测的可能值，咱们不管它就好
``````print('数据维度:', features.shape)
``````

``````# 处理时间数据
import datetime

# 分别得到年，月，日
years = features['year']
months = features['month']
days = features['day']

# datetime 格式
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for years, month, day in zip(years, months, days)]
dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]

dates[:5]
``````

[datatime.datetime(2016, 1, 1, 0, 0),
datatime.datetime(2016, 1, 2, 0, 0),
datatime.datetime(2016, 1, 3, 0, 0),
datatime.datetime(2016, 1, 4, 0, 0),
datatime.datetime(2016, 1, 5, 0, 0)]

``````# 准备画图
# 指定默认风格
plt.style.use('fivethirtyeight')

# 设置布局
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, cols=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['tenp_1'])
ax2.set_xlabel('');ax2.set_ylabel('Temperature');ax2.set_title('Previous Max Temp')

# 前天
ax3.plot(dates, features['temp_2'])
ax3.set_xlabel('Date');ax3.set_ylabel('Temperature');ax3.set_title('Two Days Prior Max Temp')

# 我的逗逼朋友
ax4.plot(dates, features['friend'])
ax4.set_xlabel('Date');ax4.set_ylabel('Temperature');ax4,set_title('Friend Estimate')

``````

``````# 独热编码
features = pd.get_dumies(features)
``````

``````labels = np.array(features['actual'])

# 在特征中去掉标签
features = features.drop('actual', axis=1)

# 名字单独保存一下，以备后患
feature_list = list(features.columns)

# 转换成合适的格式
features = np.array(features)

features,shape
``````

(348, 14)

``````from sklearn import preprocessing
input_features = preprocessing.StandardScaler().fit_transform(features)
``````

### 基于Keras构建网络模型

• activation：激活函数的选择，一般常用relu
• kernel_initializer, bias_initializer：权重与偏置参数的初始化方法，有时候不收敛换种初始化突然好使了…玄学
• kernel_regularizer, bias_regularizer：要不要加入正则化
• inputs：输入，可以自己制定，也可以让网络自动选
• units：神经元个数

``````model = tf.keras.Sequential()
``````

compile 相当于对网络进行配置，指定好优化器和损失函数等

``````# SGD 梯度下降
model.compile(optimizer=tf.keras.optimizers.SGD(0.001),
loss='mean_squared_error')
``````
``````model.fit(input_features, labels, validation_split=0.25, epochs=10, batch_size=64)
# x, y, 验证集占25%, 10轮, 优化器每次迭代64
``````

``````model.summary()
``````

### 更改初始化方法后

``````model = tf.keras.Sequential()
``````
``````model.compile(optimizer=tf.keras.optimizers.SGD(0.001),
loss='mean_squared_error')
model.fit(input_features, labels, validation_split=0.25, epochs=100, batch_size=64)
``````

### 加入正则化惩罚项

``````model = tf.keras.Sequential()

model.compile(optimizer=tf.keras.optimizers.SGD(0.001),
loss='mean_squared_error')
model.fit(input_features, labels, validation_split=0.25, epochs=100, batch_size=64)
``````

### 预测模型结果

``````predict = model.predict(input_features)

predict.shape
``````

(348, 1)

### 测试结果并进行展示

``````# 转换日期
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for years, month, day in zip(years, months, days)]
dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]

# 创建一个表格来存日期和其对应的标签数值
true_data = pd.DataFrame(data = {'date':dates, 'actual':labels})

# 同理，再创建一个来存日期和其对应的模型预测值
months = features[:, feature_list.index('month')]
days = features[:, feature_list.index('day')]
year = features[:, feature_list.index('year')]

test_dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for years, month, day in zip(years, months, days)]

tset_dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]

predications_data = pd.DataFrame(data = {'date':dates, 'actual':labels})
``````
``````# 真实值
plt.plot(ture_data['date'], true_data['actual'], 'b-', label='actual')

# 预测值
plt.plot(predictions_data['data'], predictions_data['prediction'], 'ro', label ='prediction')
plt.xticks(rotation = '60')
plt.legend()

# 图名
plt.xlabel('Date);plt.ylabel('Maximum Temperature (F)');plt.title('Actual and Predicted Values');
``````

THE END

)">