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

唐宇迪课程学习笔记

回归问题预测

  • 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')

features.head()

搭建神经网络进行气温预测
数据表中
  • year,month,day,week 分别表示具体的时间
  • temp_2:前天的最高气温
  • temp_1:昨天的最高气温
  • average:在历史中,每年这一天的平均最高温度值
  • actual:这就是我们的标签值了,当天的真实最高温度
  • friend:这一列可能是凑热闹的,你的朋友猜测的可能值,咱们不管它就好
print('数据维度:', 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 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')

plt.tight_layout(pad=2)

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

features = pd.get_dumies(features)
features.head(5)

搭建神经网络进行气温预测
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构建网络模型

下面列出了一些常见的参数:

[En]

Some common parameters have been listed, as follows:

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

按顺序构造网络模型

model = tf.keras.Sequential()
model.add(layers.Dense(16))
model.add(layers.Dense(32))
model.add(layers.Dense(1))

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


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)

搭建神经网络进行气温预测
似乎有一些问题。该模型并不完全收敛。你能调整一些参数吗?
[En]

It seems that there are some problems. The model is not completely convergent. Can you adjust some parameters?

model.summary()

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

更改初始化方法后

model = tf.keras.Sequential()
model.add(layers.Dense(16, kernel_initializer='random_normal'))
model.add(layers.Dense(32, kernel_initializer='random_normal'))
model.add(layers.Dense(1, kernel_initializer='random_normal'))
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.add(layers.Dense(16, kernel_initializer='random_normal', kernel_regularizer=tf.keras.regularizers.l2(0.03)))
model.add(layers.Dense(32, kernel_initializer='random_normal', kernel_regularizer=tf.keras.regularizers.l2(0.03)))
model.add(layers.Dense(1, kernel_initializer='random_normal', kernel_regularizer=tf.keras.regularizers.l2(0.03)))

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)

加入正则项,可以使W更加平滑

预测模型结果

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');

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

主要看验证集和测试集上的 loss 值

Original: https://blog.csdn.net/qq_51491920/article/details/124830049
Author: -素心向暖
Title: 搭建神经网络进行气温预测

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/496879/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球