【LSTM】北京pm2.5 天气预测--pytorch版本,有代码可以跑通-LSTM回归问题,工程落地一网打尽

news/2023/12/9 15:24:58

文章目录

  • 前言
  • 1. 知识理解
    • 1.1 核心理解
    • 1.2 原理
      • 1.2.1 图解LSTM
      • 1.2.1 分词
      • 1.2.1 英语的词表示
      • 1.2.2 中文的词表示
      • 1.2.3 构建词表
  • 2. 工程代码
    • 2.1 数据预处理
    • 2.2 数据集&模型构建
    • 2.3 模型训练
    • 2.4 保持模型&加载模型&预测


前言

LSTM 少分析原理,更强调工程落地,今年年初有两篇LSTM的回归文章,是keras实现的。
《【LSTM】LSTM预测股票价格–单因素、多步、输出单步回归特征 -keras 1》https://blog.csdn.net/weixin_40293999/article/details/128635150
《【LSTM】多因素单步骤预测-keras 2》http://t.csdnimg.cn/vRmMe


LSTM:做回归预测的几个应用。

1. 知识理解

1.1 核心理解

核心点:m个步长,n个因素,预测p个步长q个因素。
用前一天的日均温,预测当前天的日均温度—>1 步长 1 因素 预测 1步长 1因素
用前一天的日均温、光照时长、风速、湿度预测当前天的日均温–>1 步长 4因素 预测 1步长 1因素
用前一天的 光照时长、风速、湿度预测当前天的日均温–>1 步长 3因素 预测 1步长 1因素
用前7天的 光照时长、风速、湿度预测后三天的日均温–> 7步长 3因素 预测 3步长 1因素
用前7天的 光照时长、风速、湿度预测后三天的日均温、光照时长、风速、湿度–> 7步长 3因素 预测 3步长 4因素
通过以上的例子,相信你就能明白lstm做回归任务,能做什么。

关于其原理,自行搜索下其它人的讲解即可。本篇主要讲落地细节。

1.2 原理

1.2.1 图解LSTM

原理 ref: https://colah.github.io/posts/2015-08-Understanding-LSTMs/
RNN:
在这里插入图片描述
在这里插入图片描述
LSTM:
在这里插入图片描述
这张图挺好理解的:这个简洁来说就像个全加器一样,输出和进位都靠上一位的进位和本位的输入,挺好理解的

问题:特征 x1, x2,…xt 这t个x 对应的是什么?
就是一组特征向量,比如可以使7天的温度【一维向量 7】,也可以是7天的多维向量温度、湿度【二维向量 7X2】

1.2.1 分词

温度/湿度本身就是数字,但是若是影评数据呢?比如 当幸福来敲门的台词:
You got a dream, you gotta protect it. Dont ever let somebody tell you, you can’t do something. Not even me. People can’t do something themselves, they wanna tell you,you can’t do it.
1)去标点 2)转成全小写 3)按 “ ”【空格】分词

s = "You got a dream, you gotta protect it. Dont ever let somebody tell you, you can't  do something. Not even me. People can't do something themselves, they wanna tell you,you can't do it."
import string
print("punctuation::",string.punctuation)
for c in string.punctuation:s = s.replace(c,' ').lower()
print("after deal with punctuation::",s)
punctuation:: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
after deal with punctuation:: you got a dream  you gotta protect it  dont ever let somebody tell you  you can t  do something  not even me  people can t do something themselves  they wanna tell you you can t do it 
np.unique(s.split())
array(['a', 'can', 'do', 'dont', 'dream', 'even', 'ever', 'got', 'gotta','it', 'let', 'me', 'not', 'people', 'protect', 'somebody','something', 't', 'tell', 'themselves', 'they', 'wanna', 'you'],dtype='<U10')

然后把这些词挨个变成映射【字典】,再用100维的张量表示每一个单词即可。

1.2.1 英语的词表示

这里说的特征就是数字类的,而不是文本类的比如影评、商品评价、外卖评价等等。
多啰嗦一句,其实一个英文影评(同步类比外卖、商品评价)的单词数量,就是 x1,x2,…xt, 对应的是t个单词。但是torch只能计算,只能存储float,int…等数字类型的tensor,你这个文本算个啥,所以需要将英语表示为【数字】特征,也就是词表示【word representation】,通常使用词嵌入【word embedding】的方式。每个单词可以表示为n维度,比如200,这个可以自定义,也可以用预训练的。

1.2.2 中文的词表示

1.2.1 说清楚了英文的词表示,那么中文呢,中文和英语其实极为相似,但是最大的不同是,英语很好分词,因为天然的空格存在,按空格分词【token】,再词表示就可以,中文没空咋给一句话【一段话】分词呢?用个插件jieba即可。
例子:

jieba.lcut("你说过两天来看我,转眼就是一年多!")
['你', '说', '过', '两天', '来看', '我', ',', '转眼', '就是', '一年', '多', '!']

1.2.3 构建词表

所以无论中英文,都需要构建词表,也就是分好词的所有词的list,比如所用影评分好词后的unique词是3W个,那么我们实际上就有一个len=3W的词表,
但通常还需要另外的两个词和。因为数据对齐的问题,比如我们就想让一条评论是200【多少的长度是自己定的】个单词。 那多了,就截断了,少的就用填充。
另外,还需要一个注意点,就是从set【集合】的角度看,词表有3W个,但里面可能有只出现过1次的,他们可能是生僻词,或者拼写错误的,没啥具体含义。所以,做映射的时候,可能制取词频>=10的。那么没取到的,就被映射为unknow了。
然后,再用1.2的词表示。 这样,一条评论。最终就是 200个单词, 每个单词用100维的向量【数字】来表示。这样就和1.1的原理完全对上了。

2. 工程代码

2.1 数据预处理

在这里插入图片描述
pandas 读取数据,并完成预处理
No year month day hour pm2.5 DEWP TEMP PRES cbwd Iws Is Ir
0 1 2010 1 1 0 NaN -21 -11.0 1021.0 NW 1.79 0 0
1 2 2010 1 1 1 NaN -21 -12.0 1020.0 NW 4.92 0 0
2 3 2010 1 1 2 NaN -21 -11.0 1019.0 NW 6.71 0 0
3 4 2010 1 1 3 NaN -21 -14.0 1019.0 NW 9.84 0 0
4 5 2010 1 1 4 NaN -20 -12.0 1018.0 NW 12.97 0 0

数据处理:把PM2.5 为null的数据都用相邻的数据填充,我们取2010年1月2日以后的数据。

data = data.iloc[24:].bfill()
print(data[0:5])

在这里插入图片描述
把年,月,日 和小时 合并为一列。

import datetime
data['time'] = data.apply(lambda x: datetime.datetime(year=x['year'],month =x['month'],day = x['day'],hour = x['hour']),axis = 1)
    No  year  month  day  hour  pm2.5  DEWP  TEMP    PRES cbwd   Iws  Is  Ir                time
24  25  2010      1    2     0  129.0   -16  -4.0  1020.0   SE  1.79   0   0 2010-01-02 00:00:00
25  26  2010      1    2     1  148.0   -15  -4.0  1020.0   SE  2.68   0   0 2010-01-02 01:00:00
26  27  2010      1    2     2  159.0   -11  -5.0  1021.0   SE  3.57   0   0 2010-01-02 02:00:00
27  28  2010      1    2     3  181.0    -7  -5.0  1022.0   SE  5.36   1   0 2010-01-02 03:00:00
28  29  2010      1    2     4  138.0    -7  -5.0  1022.0   SE  6.25   2   0 2010-01-02 04:00:

去掉 年,月,日 和小时,并且把 时间列 作为索引index

data.drop(columns=['No','year','month','day','hour'],inplace = True)
data = data.set_index('time')
                     pm2.5  DEWP  TEMP    PRES cbwd   Iws  Is  Ir
time
2010-01-02 00:00:00  129.0   -16  -4.0  1020.0   SE  1.79   0   0
2010-01-02 01:00:00  148.0   -15  -4.0  1020.0   SE  2.68   0   0
2010-01-02 02:00:00  159.0   -11  -5.0  1021.0   SE  3.57   0   0
2010-01-02 03:00:00  181.0    -7  -5.0  1022.0   SE  5.36   1   0
2010-01-02 04:00:00  138.0    -7  -5.0  1022.0   SE  6.25   2   0

One-hot 编码 风向序列

data = data.join(pd.get_dummies(data.cbwd))
del data['cbwd']
                     pm2.5  DEWP  TEMP    PRES   Iws  Is  Ir     NE     NW    SE     cv
time
2010-01-02 00:00:00  129.0   -16  -4.0  1020.0  1.79   0   0  False  False  True  False
2010-01-02 01:00:00  148.0   -15  -4.0  1020.0  2.68   0   0  False  False  True  False
2010-01-02 02:00:00  159.0   -11  -5.0  1021.0  3.57   0   0  False  False  True  False
2010-01-02 03:00:00  181.0    -7  -5.0  1022.0  5.36   1   0  False  False  True  False
2010-01-02 04:00:00  138.0    -7  -5.0  1022.0  6.25   2   0  False  False  True  False

查看2012年到2014年的数据

data['pm2.5'][-365*24:].plot()
data['pm2.5'][-365*24*2:-365*24].plot()
data['pm2.5'][-365*24*3:-365*24*2].plot()

在这里插入图片描述
用前6天的数据预测第7天的大气PM2.5

sequence_length = 6*24
delay = 24
data_ = []
for i in range(len(data) - sequence_length - delay):data_.append(data.iloc[i: i + sequence_length + delay])
data_ = np.array([df.values for df in data_])
np.random.shuffle(data_)
x = data_[:, :-delay, :]
y = data_[:, -1, 0]

把数据的80%分成训练集合,20%分为测试集合。

split_boundary = int(data_.shape[0] * 0.8)
train_x = x[: split_boundary]
test_x = x[split_boundary:]train_y = y[: split_boundary]
test_y = y[split_boundary:]

对数据标准化

mean = train_x.mean(axis=0) #均值
std = train_x.std(axis=0) #标准差
train_x = (train_x - mean)/std
test_x = (test_x - mean)/std

2.2 数据集&模型构建


class Mydataset(torch.utils.data.Dataset):def __init__(self, features, labels):self.features = featuresself.labels = labelsdef __getitem__(self, index):feature = self.features[index]label = self.labels[index]return feature, labeldef __len__(self):return len(self.features)
train_ds = Mydataset(train_x, train_y)
test_ds = Mydataset(test_x, test_y)
BTACH_SIZE = 128
train_dl = torch.utils.data.DataLoader(train_ds,batch_size=BTACH_SIZE,shuffle=True
)
test_dl = torch.utils.data.DataLoader(test_ds,batch_size=BTACH_SIZE
)

构建模型

hidden_size = 64
class Net(nn.Module):def __init__(self, hidden_size):super(Net, self).__init__()self.rnn = nn.LSTM(train_x.shape[-1], hidden_size, batch_first=True)self.fc1 = nn.Linear(hidden_size, 128)self.fc2 = nn.Linear(128, 1)def forward(self, inputs):_, s_o = self.rnn(inputs)s_o = s_o[-1]x = F.dropout(F.relu(self.fc1(s_o)))x = self.fc2(x)return torch.squeeze(x)
model = Net(hidden_size)
if torch.cuda.is_available():model.to('cuda')

构建损失和优化函数


loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

2.3 模型训练

训练过程

def fit(epoch, model, trainloader, testloader):total = 0running_loss = 0model.train()for x, y in trainloader:if torch.cuda.is_available():x, y = x.to('cuda'), y.to('cuda')y_pred = model(x)loss = loss_fn(y_pred, y)optimizer.zero_grad()loss.backward()optimizer.step()with torch.no_grad():total += y.size(0)running_loss += loss.item()
#    exp_lr_scheduler.step()epoch_loss = running_loss / len(trainloader.dataset)test_total = 0test_running_loss = 0 model.eval()with torch.no_grad():for x, y in testloader:if torch.cuda.is_available():x, y = x.to('cuda'), y.to('cuda')y_pred = model(x)loss = loss_fn(y_pred, y)test_total += y.size(0)test_running_loss += loss.item()epoch_test_loss = test_running_loss / len(testloader.dataset)print('epoch: ', epoch, 'loss: ', round(epoch_loss, 3),'test_loss: ', round(epoch_test_loss, 3),)return epoch_loss, epoch_test_loss
epochs = 100
train_loss = []
test_loss = []for epoch in range(epochs):epoch_loss, epoch_test_loss = fit(epoch,model,train_dl,test_dl)train_loss.append(epoch_loss)test_loss.append(epoch_test_loss)

训练过程loss


epoch:  0 loss:  23.613 test_loss:  25.115
epoch:  1 loss:  23.081 test_loss:  24.546
epoch:  2 loss:  22.261 test_loss:  23.605
epoch:  3 loss:  21.603 test_loss:  23.745
epoch:  4 loss:  21.623 test_loss:  24.013
epoch:  5 loss:  21.449 test_loss:  24.356
epoch:  6 loss:  21.052 test_loss:  22.461
epoch:  7 loss:  21.267 test_loss:  24.883
epoch:  8 loss:  21.083 test_loss:  21.641
epoch:  9 loss:  20.027 test_loss:  24.942
epoch:  10 loss:  19.944 test_loss:  20.995
epoch:  11 loss:  20.05 test_loss:  23.553
epoch:  12 loss:  30.013 test_loss:  29.03
epoch:  13 loss:  23.522 test_loss:  22.274
epoch:  14 loss:  20.181 test_loss:  21.099
epoch:  15 loss:  19.553 test_loss:  20.401
epoch:  16 loss:  18.925 test_loss:  21.033
epoch:  17 loss:  18.798 test_loss:  19.627
epoch:  18 loss:  19.772 test_loss:  20.952
epoch:  19 loss:  19.922 test_loss:  20.91
epoch:  20 loss:  19.068 test_loss:  20.825
epoch:  21 loss:  18.103 test_loss:  19.203
epoch:  22 loss:  19.176 test_loss:  20.891
epoch:  23 loss:  17.713 test_loss:  19.167
epoch:  24 loss:  17.063 test_loss:  18.672
epoch:  25 loss:  19.715 test_loss:  23.334
epoch:  26 loss:  21.586 test_loss:  20.307
epoch:  27 loss:  18.127 test_loss:  19.236
epoch:  28 loss:  16.943 test_loss:  18.996
epoch:  29 loss:  17.403 test_loss:  19.15
epoch:  30 loss:  16.35 test_loss:  18.142
epoch:  31 loss:  16.166 test_loss:  18.056
epoch:  32 loss:  16.363 test_loss:  20.465
epoch:  33 loss:  16.122 test_loss:  17.937
epoch:  34 loss:  15.48 test_loss:  17.128
epoch:  35 loss:  17.159 test_loss:  19.565
epoch:  36 loss:  18.402 test_loss:  22.737
epoch:  37 loss:  17.671 test_loss:  19.016
epoch:  38 loss:  16.368 test_loss:  17.944
epoch:  39 loss:  15.901 test_loss:  18.256
epoch:  40 loss:  15.695 test_loss:  18.299
epoch:  41 loss:  15.447 test_loss:  16.485
epoch:  42 loss:  14.995 test_loss:  16.351
epoch:  43 loss:  14.906 test_loss:  17.371
epoch:  44 loss:  14.784 test_loss:  16.312
epoch:  45 loss:  15.204 test_loss:  17.165
epoch:  46 loss:  15.076 test_loss:  16.702
epoch:  47 loss:  14.528 test_loss:  15.929
epoch:  48 loss:  14.185 test_loss:  31.667
epoch:  49 loss:  22.848 test_loss:  20.964

2.4 保持模型&加载模型&预测

# 模型参数保存
torch.save(model.state_dict(), 'model_param.pt')
# 模型参数加载
model = Net(...)
model.load_state_dict(torch.load('model_param.pt'))
data_test = data[-24*6:]
data_test = (data_test - mean)/std
data_test = data_test.to_numpy()
data_test = np.expand_dims(data_test,0)
pm = model(torch.from_numpy(data_test).float().cuda())

在这里插入图片描述
在这里插入图片描述

ref: https://www.aqistudy.cn/historydata/daydata.php?city=%E5%8C%97%E4%BA%AC&month=2015-01


http://www.ppmy.cn/news/1230883.html

相关文章

Vue3 常用组件

一、Fragment组件 Vue2 的template 模板中必须要有一个根标签&#xff0c;而我们在Vue3 的模板中不需要使用根标签就能渲染&#xff0c;因为Vue3 在内部会将多个标签包含在一个Fragment 虚拟元素中。 好处就在于可以减少标签的层级&#xff0c;减小内存占用。 二、Teleport组…

HR问:有没有免费的人才测评工具?

人才测评工具分为两种&#xff0c;一种是测评量表&#xff0c;一种是操作量表的工具&#xff0c;在线测评的方式没有普及之前&#xff0c;很多朋友都习惯把测评量表&#xff08;测评试题&#xff09;称为测评工具&#xff0c;其实我认为量表就是量表&#xff0c;而试试量表测评…

Spark---核心介绍

一、Spark核心 1、RDD 1&#xff09;、概念&#xff1a; RDD&#xff08;Resilient Distributed Datest&#xff09;&#xff0c;弹性分布式数据集。 2&#xff09;、RDD的五大特性&#xff1a; 1、RDD是由一系列的partition组成的 2、函数是作用在每一个partition(split…

平衡二叉树c语言版

一、定义二叉树结点结构体 /*** 定义平衡二叉树结点 */ struct avlbinarytree { //数据域NodeData* data;///树高int h;struct avlbinarytree* left;struct avlbinarytree* right; }; typedef struct avlbinarytree AVLNode; 二、声明函数的操作 /*** 创建结点 */ AV…

PP-PicoDet算法训练行人检测模型

PP-PicoDet算法训练行人检测模型 1&#xff0c;效果图2&#xff0c;PP-PicoDet介绍3&#xff0c;使用飞浆框架训练模型1&#xff0c;准备好图片和对应的标注文件2&#xff0c;划分训练集和验证集3&#xff0c;vi label_list.txt4&#xff0c;目录结构5&#xff0c;修改配置文件…

Android VSYNC发展历程

0 前言 安卓直到android-4.1.1_r1才首次引入VSYNC实现&#xff0c;然后逐步演进到android-4.4才得以完善&#xff0c;并在android-11、12后继续大改。 1 尚未引入 android-4.0.4_r2.1之前尚未引入VSYNC[1]&#xff0c;SurfaceFlinger被实现为一个线程&#xff0c;通过睡眠来实…

【Django-DRF用法】多年积累md笔记,第3篇:Django-DRF的序列化和反序列化详解

本文从分析现在流行的前后端分离Web应用模式说起&#xff0c;然后介绍如何设计REST API&#xff0c;通过使用Django来实现一个REST API为例&#xff0c;明确后端开发REST API要做的最核心工作&#xff0c;然后介绍Django REST framework能帮助我们简化开发REST API的工作。 全…

请求prometheus数据然后使用tensorflow计算正则模型

使用tensorflow 计算正则模型, 数据来源为Prometheus的数据近7天的数据, 然后计算了90%区间的上下限和当前值的概率密度 import requests import pandas as pd import tensorflow as tf from datetime import datetime, timedelta# 定义 Prometheus 查询的参数 url "htt…

钩子函数-hook

钩子函数-hook hook 的作用 利用钩子函数可以在所有测试用例执行前做一些预置操作&#xff08;如&#xff1a;准被测试数据、测试环境&#xff09; 或者在测试结束后做一些后置操作&#xff08;如&#xff1a;清理测试数据&#xff09; 钩子函数在其它框架中也有&#xff0…

uni-app:前端实现心跳机制(全局)+局部页面控制心跳暂停和重新心跳

一、App.vue全局中写入心跳 在data中定义变量heartbeatTimer&#xff0c;便于暂停心跳使用在onLaunch中引用开始心跳的方法startHeartbeat()写入开始心跳方法写入暂停心跳方法写入请求后端刷心跳机制 定义变量 // 在全局设置的心跳机制中添加一个变量来保存定时器的标识 data(…

HALCON根据需要创建自定义函数

任务要求&#xff1a; 创建函数myfun(a,b,c)&#xff0c;输入浮点数a&#xff0c;b的值&#xff0c;计算c a b&#xff0c;将计算结果返回。 操作步骤&#xff1a; 1&#xff09;打开HDevelop程序 2&#xff09;打开函数菜单&#xff0c;选择“创建新函数”&#xff0c…

torch 的数据加载 Datasets DataLoaders

点赞收藏关注&#xff01; 如需要转载&#xff0c;请注明出处&#xff01; torch的模型加载有两种方式&#xff1a; Datasets & DataLoaders torch本身可以提供两数据加载函数&#xff1a; torch.utils.data.DataLoader&#xff08;&#xff09;和torch.utils.data.Datase…

11 redis中分布式锁的实现

单机锁代码 import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.con…

如何解决requests库自动确定认证arded 类型

requests 库是一种非常强大的爬虫工具,可以用于快速构建高效和稳定的网络爬虫程序。对于经常使用爬虫IP用来网站爬虫反爬策略的我来说&#xff0c;下面遇到的问题应当值得我们思考一番。 问题背景 在使用requests库进行网络请求时&#xff0c;有时会遇到需要对目标服务进行认证…

c++模式之单例模式详解

c模式之单例模式详解 1.概念2.懒汉模式示例&#xff08;缺点&#xff09;3.懒汉模式线程安全4.饿汉式创建单例5.饿汉模式线程示例 1.概念 单例模式是指在整个系统生命周期内&#xff0c;保证一个类只能产生一个实例&#xff0c;确保该类的唯一性. 使用单例两个原因&#xff1a…

在 Windows 中关闭 Nginx 所有进程

在 Windows 中关闭 Nginx 所有进程并强制重启的命令如下&#xff1a; 打开命令提示符&#xff08;CMD&#xff09;。 输入以下命令来查找 Nginx 进程的 PID&#xff1a; tasklist /fi "imagename eq nginx.exe"此命令将列出所有名为 nginx.exe 的进程以及它们的 PID…

Visio免费版!Visio国产平替软件,终于被我找到啦!

作为一个职场人士&#xff0c;我经常需要绘制各种流程图和图表&#xff0c;而Visio一直是我使用的首选工具。但是&#xff0c;随着公司的发展和工作的需要&#xff0c;我逐渐发现了Visio的优点和不足。 首先&#xff0c;让我们来看看Visio的优点。Visio是一个专业的流程图和图…

JVS低代码表单设计:数据联动详解(多级数据级、数据回显等)

在这信息化时代&#xff0c;表单作为数据的收集和展示工具&#xff0c;已经渗透到不同的角落。JVS低代码对表单的设计和操作进行了不断的优化和创新。其中&#xff0c;联动回显作为一项重要的功能&#xff0c;无论是多级数据级联控制、组件的联动控制&#xff0c;还是多表的数据…

数据中心走向绿色低碳,液冷存储舍我其谁

引言&#xff1a;没有最冷&#xff0c;只有更冷&#xff0c;绿色低碳早已成为行业关键词。 【全球存储观察 &#xff5c; 科技热点关注】 每一次存储行业的创新&#xff0c;其根源离不开行业端的用户需求驱动。 近些年从数据中心建设的整体发展情况来看&#xff0c;从风冷到…

基于单片机GPS轨迹定位和里程统计系统

**单片机设计介绍&#xff0c; 基于单片机GPS轨迹定位和里程统计系统 文章目录 一 概要二、功能设计设计思路 三、 软件设计原理图 五、 程序六、 文章目录 一 概要 一个基于单片机、GPS和里程计的轨迹定位和里程统计系统可以被设计成能够在移动的交通工具中精确定位车辆的位置…
最新文章