PyTorch一小时掌握之神经网络气温预测篇

作者:我是小白呀 时间:2022-07-30 23:52:19 

概述

具体的案例描述在此就不多赘述. 同一数据集我们在机器学习里的随机森林模型中已经讨论过.

导包


import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
from sklearn.preprocessing import StandardScaler
import torch

数据读取


# ------------------1. 数据读取------------------

# 读取数据
data = pd.read_csv("temps.csv")

# 看看数据长什么样子
print(data.head())

# 查看数据维度
print("数据维度:", data.shape)

# 产看数据类型
print("数据类型:", type(data))

输出结果:
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
数据维度: (348, 9)
数据类型: <class 'pandas.core.frame.DataFrame'>

数据预处理


# ------------------2. 数据预处理------------------

# datetime 格式
dates = pd.PeriodIndex(year=data["year"], month=data["month"], day=data["day"], freq="D").astype(str)
dates = [datetime.datetime.strptime(date, "%Y-%m-%d") for date in dates]
print(dates[:5])

# 编码转换
data = pd.get_dummies(data)
print(data.head())

# 画图
plt.style.use("fivethirtyeight")
register_matplotlib_converters()

# 标签
labels = np.array(data["actual"])

# 取消标签
data = data.drop(["actual"], axis= 1)
print(data.head())

# 保存一下列名
feature_list = list(data.columns)

# 格式转换
data_new = np.array(data)

data_new  = StandardScaler().fit_transform(data_new)
print(data_new[: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)]
year month day temp_2 ... week_Sun week_Thurs week_Tues week_Wed
0 2016 1 1 45 ... 0 0 0 0
1 2016 1 2 44 ... 0 0 0 0
2 2016 1 3 45 ... 1 0 0 0
3 2016 1 4 44 ... 0 0 0 0
4 2016 1 5 41 ... 0 0 1 0

[5 rows x 15 columns]
year month day temp_2 ... week_Sun week_Thurs week_Tues week_Wed
0 2016 1 1 45 ... 0 0 0 0
1 2016 1 2 44 ... 0 0 0 0
2 2016 1 3 45 ... 1 0 0 0
3 2016 1 4 44 ... 0 0 0 0
4 2016 1 5 41 ... 0 0 1 0

[5 rows x 14 columns]
[[ 0. -1.5678393 -1.65682171 -1.48452388 -1.49443549 -1.3470703
-1.98891668 2.44131112 -0.40482045 -0.40961596 -0.40482045 -0.40482045
-0.41913682 -0.40482045]
[ 0. -1.5678393 -1.54267126 -1.56929813 -1.49443549 -1.33755752
0.06187741 -0.40961596 -0.40482045 2.44131112 -0.40482045 -0.40482045
-0.41913682 -0.40482045]
[ 0. -1.5678393 -1.4285208 -1.48452388 -1.57953835 -1.32804474
-0.25855917 -0.40961596 -0.40482045 -0.40961596 2.47023092 -0.40482045
-0.41913682 -0.40482045]
[ 0. -1.5678393 -1.31437034 -1.56929813 -1.83484692 -1.31853195
-0.45082111 -0.40961596 2.47023092 -0.40961596 -0.40482045 -0.40482045
-0.41913682 -0.40482045]
[ 0. -1.5678393 -1.20021989 -1.8236209 -1.91994977 -1.30901917
-1.2198689 -0.40961596 -0.40482045 -0.40961596 -0.40482045 -0.40482045
2.38585576 -0.40482045]]

构建网络模型


# ------------------3. 构建网络模型------------------

x = torch.tensor(data_new)
y = torch.tensor(labels)

# 权重参数初始化
weights1 = torch.randn((14,128), dtype=float, requires_grad= True)
biases1 = torch.randn(128, dtype=float, requires_grad= True)
weights2 = torch.randn((128,1), dtype=float, requires_grad= True)
biases2 = torch.randn(1, dtype=float, requires_grad= True)

learning_rate = 0.001
losses = []

for i in range(1000):
   # 计算隐层
   hidden = x.mm(weights1) + biases1
   # 加入激活函数
   hidden = torch.relu(hidden)
   # 预测结果
   predictions = hidden.mm(weights2) + biases2
   # 计算损失
   loss = torch.mean((predictions - y) ** 2)

# 打印损失值
   if i % 100 == 0:
       print("loss:", loss)
   # 反向传播计算
   loss.backward()

# 更新参数
   weights1.data.add_(-learning_rate * weights1.grad.data)
   biases1.data.add_(-learning_rate * biases1.grad.data)
   weights2.data.add_(-learning_rate * weights2.grad.data)
   biases2.data.add_(-learning_rate * biases2.grad.data)

# 每次迭代清空
   weights1.grad.data.zero_()
   biases1.grad.data.zero_()
   weights2.grad.data.zero_()
   biases2.grad.data.zero_()

输出结果:
loss: tensor(4746.8598, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(156.5691, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(148.9419, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(146.1035, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(144.5652, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(143.5376, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(142.7823, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(142.2151, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(141.7770, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(141.4294, dtype=torch.float64, grad_fn=<MeanBackward0>)

数据可视化


# ------------------4. 数据可视化------------------

def graph1():
   # 创建子图
   f, ax = plt.subplots(2, 2, figsize=(10, 10))

# 标签值
   ax[0, 0].plot(dates, labels, color="#ADD8E6")
   ax[0, 0].set_xticks([""])
   ax[0, 0].set_ylabel("Temperature")
   ax[0, 0].set_title("Max Temp")

# 昨天
   ax[0, 1].plot(dates, data["temp_1"], color="#87CEFA")
   ax[0, 1].set_xticks([""])
   ax[0, 1].set_ylabel("Temperature")
   ax[0, 1].set_title("Previous Max Temp")

# 前天
   ax[1, 0].plot(dates, data["temp_2"], color="#00BFFF")
   ax[1, 0].set_xticks([""])
   ax[1, 0].set_xlabel("Date")
   ax[1, 0].set_ylabel("Temperature")
   ax[1, 0].set_title("Two Days Prior Max Temp")

# 朋友
   ax[1, 1].plot(dates, data["friend"], color="#1E90FF")
   ax[1, 1].set_xticks([""])
   ax[1, 1].set_xlabel("Date")
   ax[1, 1].set_ylabel("Temperature")
   ax[1, 1].set_title("Friend Estimate")

plt.show()

输出结果:

PyTorch一小时掌握之神经网络气温预测篇

完整代码


import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
from sklearn.preprocessing import StandardScaler
import torch

# ------------------1. 数据读取------------------

# 读取数据
data = pd.read_csv("temps.csv")

# 看看数据长什么样子
print(data.head())

# 查看数据维度
print("数据维度:", data.shape)

# 产看数据类型
print("数据类型:", type(data))

# ------------------2. 数据预处理------------------

# datetime 格式
dates = pd.PeriodIndex(year=data["year"], month=data["month"], day=data["day"], freq="D").astype(str)
dates = [datetime.datetime.strptime(date, "%Y-%m-%d") for date in dates]
print(dates[:5])

# 编码转换
data = pd.get_dummies(data)
print(data.head())

# 画图
plt.style.use("fivethirtyeight")
register_matplotlib_converters()

# 标签
labels = np.array(data["actual"])

# 取消标签
data = data.drop(["actual"], axis= 1)
print(data.head())

# 保存一下列名
feature_list = list(data.columns)

# 格式转换
data_new = np.array(data)

data_new  = StandardScaler().fit_transform(data_new)
print(data_new[:5])

# ------------------3. 构建网络模型------------------

x = torch.tensor(data_new)
y = torch.tensor(labels)

# 权重参数初始化
weights1 = torch.randn((14,128), dtype=float, requires_grad= True)
biases1 = torch.randn(128, dtype=float, requires_grad= True)
weights2 = torch.randn((128,1), dtype=float, requires_grad= True)
biases2 = torch.randn(1, dtype=float, requires_grad= True)

learning_rate = 0.001
losses = []

for i in range(1000):
   # 计算隐层
   hidden = x.mm(weights1) + biases1
   # 加入激活函数
   hidden = torch.relu(hidden)
   # 预测结果
   predictions = hidden.mm(weights2) + biases2
   # 计算损失
   loss = torch.mean((predictions - y) ** 2)

# 打印损失值
   if i % 100 == 0:
       print("loss:", loss)
   # 反向传播计算
   loss.backward()

# 更新参数
   weights1.data.add_(-learning_rate * weights1.grad.data)
   biases1.data.add_(-learning_rate * biases1.grad.data)
   weights2.data.add_(-learning_rate * weights2.grad.data)
   biases2.data.add_(-learning_rate * biases2.grad.data)

# 每次迭代清空
   weights1.grad.data.zero_()
   biases1.grad.data.zero_()
   weights2.grad.data.zero_()
   biases2.grad.data.zero_()

# ------------------4. 数据可视化------------------

def graph1():
   # 创建子图
   f, ax = plt.subplots(2, 2, figsize=(10, 10))

# 标签值
   ax[0, 0].plot(dates, labels, color="#ADD8E6")
   ax[0, 0].set_xticks([""])
   ax[0, 0].set_ylabel("Temperature")
   ax[0, 0].set_title("Max Temp")

# 昨天
   ax[0, 1].plot(dates, data["temp_1"], color="#87CEFA")
   ax[0, 1].set_xticks([""])
   ax[0, 1].set_ylabel("Temperature")
   ax[0, 1].set_title("Previous Max Temp")

# 前天
   ax[1, 0].plot(dates, data["temp_2"], color="#00BFFF")
   ax[1, 0].set_xticks([""])
   ax[1, 0].set_xlabel("Date")
   ax[1, 0].set_ylabel("Temperature")
   ax[1, 0].set_title("Two Days Prior Max Temp")

# 朋友
   ax[1, 1].plot(dates, data["friend"], color="#1E90FF")
   ax[1, 1].set_xticks([""])
   ax[1, 1].set_xlabel("Date")
   ax[1, 1].set_ylabel("Temperature")
   ax[1, 1].set_title("Friend Estimate")

plt.show()

if __name__ == "__main__":
   graph1()

来源:https://blog.csdn.net/weixin_46274168/article/details/114176589

标签:PyTorch,神经网络气温预测,python
0
投稿

猜你喜欢

  • numpy实现神经网络反向传播算法的步骤

    2021-02-11 10:54:34
  • Django模型验证器介绍与源码分析

    2023-10-19 13:49:53
  • python实现学生信息管理系统

    2021-11-27 11:46:42
  • Django--权限Permissions的例子

    2021-02-16 01:44:51
  • Tensorflow使用支持向量机拟合线性回归

    2021-01-20 03:38:42
  • IE下中英文字体不能对齐原因及解决

    2008-08-11 12:47:00
  • 如何做迅雷电影提示效果

    2011-03-31 17:15:00
  • 如何处理json中不带双引号的key的问题

    2023-02-07 00:25:22
  • 教你用Python实现一个轮盘抽奖小游戏

    2021-11-04 23:49:03
  • Python 列表排序详解

    2022-08-14 05:05:21
  • 使用cookie和application实现在线人数统计

    2007-09-18 13:01:00
  • python文件操作的基础详细讲解(write、read、readlines、readline)

    2021-04-05 19:31:44
  • Python获取时间范围内日期列表和周列表的函数

    2023-03-25 15:16:39
  • Python运维开发之psutil库的使用详解

    2023-10-24 14:22:55
  • 基于Python中单例模式的几种实现方式及优化详解

    2022-10-24 14:20:45
  • asp生成不需要数据库的中奖码

    2008-07-18 12:31:00
  • 介绍Python中的一些高级编程技巧

    2022-09-22 19:23:15
  • Python列表list内建函数用法实例分析【insert、remove、index、pop等】

    2022-01-13 10:18:24
  • python爬取基于m3u8协议的ts文件并合并

    2021-11-03 16:44:45
  • Numpy np.array()函数使用方法指南

    2023-05-23 09:33:37
  • asp之家 网络编程 m.aspxhome.com