4-3 Working with time series

news2024/9/21 10:59:19

本文所用数据下载

Data from a Washington, D.C., bike-sharing system reporting the hourly count of rental bikes in 2011–2012 in the Capital Bikeshare system, along with weather and seasonal information.

在这里插入图片描述

Our goal will be to take a flat, 2D dataset and transform it into a 3D one, as shown in figure 4.5.
(figure 4.5 shows a transposed version of this to better fit on the printed page)
We want to change the row-per-hour organization so that we have one axis that increases at a rate of one day per index increment, and another axis that represents the hour of the day (independent of the date). The third axis will be our different columns of data (weather, temperature, and so on).

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

import numpy as np
bikes_numpy=np.loadtxt("D:/bike-sharing-dataset/hour-fixed.csv",dtype=np.float32,delimiter=",",skiprows=1,converters={1:lambda x:float(x[8:10])})

文件中的数据采用逗号分隔,跳过了第一行(标题行),并对第二列(第1个索引,由 0 开始)的数据进行了特殊的转换,使用了一个 lambda 函数,将字符串索引为 8 和 9的位置转换为浮点数。

回顾:lambda 函数

在这里插入图片描述
每一个日期是10位数,格式为2011-01-01,索引从0起
因此将“日”位转化为了浮点数

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

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

每一行是一行数据
在这里插入图片描述

然后将其转化为tensor

import torch
bikes=torch.from_numpy(bikes_numpy)
bikes

在这里插入图片描述

bikes.shape, bikes.stride()

在这里插入图片描述

关于表格中部分标题的说明
Season: season (1: spring, 2: summer, 3: fall, 4: winter)
Year: yr (0: 2011, 1: 2012)
Month: mnth (1 to 12)
Hour: hr (0 to 23)
Weather situation: weathersit (1: clear, 2:mist, 3: light rain/snow, 4: heavy rain/snow)
Temperature in °C: temp
Perceived temperature in °C: atemp
Humidity: hum
Wind speed: windspeed
Number of casual users: casual
Number of registered users: registered
Count of rental bikes: cnt

从表格可以看出,我们对每天的24小时进行了监测。一共有17,520小时,17列

Now let’s reshape the data to have 3 axes—day, hour, and then our 17 columns:

daily_bikes=bikes.view(-1,24,bikes.shape[1])
daily_bikes.shape

在这里插入图片描述

We use -1 as a placeholder for “however many indexes are left, given the other dimensions and the original number of elements.”
-1 表示根据其他维度的大小自动计算新的维度大小,以保持总元素数量不变。在这里,它会计算为 730,表示将原始 17520 个小时的数据合并为 730 组,每组包含 24 个小时的数据。(17520小时/每天24小时=730天)

daily_bikes.stride()

在这里插入图片描述
对于408:按天间隔,一行17个,一天24小时,17×24=408

在这里插入图片描述
As you learned in the previous chapter, calling view on a tensor returns a new tensor that changes the number of dimensions and the striding information, without changing the storage. This means we can rearrange our tensor at basically zero cost, because no data will be copied.

We see that the rightmost dimension is the number of columns in the original dataset. Then, in the middle dimension, we have time, split into chunks of 24 sequential hours. In other words, we now have N sequences of L hours in a day, for C channels.

我们得到N个长度为L的C序列集合。换句话说,我们的时间序列数据集将是一个维数为3的张量,形状为N×L×C。C将保持我们的17个通道,而L将是一天中的24小时,因此N表示天数730

To get to our desired N × C × L ordering, we need to transpose the tensor:

daily_bikes=daily_bikes.transpose(1,2)
daily_bikes.shape,daily_bikes.stride()

在这里插入图片描述
Now let’s apply some of the techniques we learned earlier to this dataset.

In order to make it easier to render our data, we’re going to limit ourselves to the first day for a moment. We initialize a zero-filled matrix with a number of rows equal to the number of hours in the day and number of columns equal to the number of weather levels:

first_day=bikes[:24].long()
first_day.shape

在这里插入图片描述

If we decided to go with categorical, we would turn the variable into a one-hot-encoded vector and concatenate the columns with the dataset.
我们关注weather situation列,取值为0~3,所以下面矩阵给4列

weather_onehot=torch.zeros(first_day.shape[0],4)
weather_onehot

在这里插入图片描述

first_day[:,9]

在这里插入图片描述
Then we scatter ones into our matrix according to the corresponding level at each row.

weather_onehot.scatter_(dim=1,index=first_day[:,9].unsqueeze(1).long()-1,value=1.0)
# Decreases the values by 1 because weather situation ranges from 1 to 4, while indices are 0-based

在这里插入图片描述
Last, we concatenate our matrix to our original dataset using the cat function.
Let’s look at the first of our results:

torch.cat((bikes[:24],weather_onehot),1)[:1]

bikes[:24]取第一天24小时的所有数据
torch.cat 函数将bikes[:24](24×17)和weather_onehot(24×4)在第1维度(列维度)进行连接
注:列维度拼接可以看做是“为增加列”而进行的拼接,是左右拼接的
在这里插入图片描述

[:1] 是取连接后张量的第0行
在这里插入图片描述
We could have done the same with the reshaped daily_bikes tensor.

daily_bikes.shape

在这里插入图片描述

daily_weather_onehot=torch.zeros(daily_bikes.shape[0],4,daily_bikes.shape[2])
daily_weather_onehot[0]

在这里插入图片描述

daily_weather_onehot.scatter_(1,daily_bikes[:,9,:].long().unsqueeze(1)-1,1.0)
daily_weather_onehot[0]

在这里插入图片描述
daily_weather_onehot[0]表示第一天24小时的数据,每行用1.0表示weathersit为该行的小时序号,如第一行表示weathersit为1的小时序号,第二行表示weathersit为2的小时序号

在这里插入图片描述

daily_bikes.shape,daily_weather_onehot.shape

在这里插入图片描述

daily_bikes=torch.cat((daily_bikes,daily_weather_onehot),dim=1)
daily_bikes[0]

在这里插入图片描述
(续)完成行拼接
在这里插入图片描述

We mentioned earlier that this is not the only way to treat our “weather situation” variable. Indeed, its labels have an ordinal relationship, so we could pretend they are special values of a continuous variable. We could just transform the variable so that it runs from 0.0 to 1.0:(数据标准化)

方法一:减最小值/范围
范围指数据最大值和最小值的差
例如:上述weathersit的取值为1~3(可取值为1 ~ 4)

daily_bikes[:,9,:]=(daily_bikes[:,9,:]-1.0)/3

通过这种方式就可也将数据限制在0到1

应用到数据同样,例如我们用temp行(第10行)

temp=daily_bikes[:,10,:]
temp_min=torch.min(temp)
temp_max=torch.max(temp)
daily_bikes[:,10,:]=(daily_bikes[:,10,:]-temp_min)/(temp_max-temp_min)

方法二:减去平均值,除以标准差

temp=daily_bikes[:,10,:]
daily_bikes[:,10,:]=(daily_bikes[:,10,:]-torch.mean(temp))/torch.std(temp)

Rescaling variables to the [0.0, 1.0] interval or the [-1.0, 1.0] interval is something we’ll want to do for all quantitative variables, like temperature (column 10 in our dataset). We’ll see why later; for now, let’s just say that this is beneficial to the training process.

We’ve built another nice dataset, and we’ve seen how to deal with time series data.

总结:
本文主要介绍了如何处理时间序列数据。在前一节中,我们介绍了如何表示组织在平面表中的数据。每一行都是独立的,它们的顺序并不重要,或者等效地说,没有列编码信息表明哪些行先出现,哪些后出现。

接着,我们引入了一个新的数据集,即华盛顿特区的自行车共享系统数据集。该数据集报告了2011年至2012年期间首都自行车共享系统每小时的租赁自行车数量,以及天气和季节信息。我们将这个平面的2D数据集转换为一个3D的时间序列数据集。时间序列数据集是一个三维张量,其中一个维度表示天数,另一个维度表示一天中的小时,第三个维度表示数据的不同列(例如天气、温度等)。

为了实现这个目标,我们首先将原始数据集转换为一个3D张量,其中每个小时的数据被堆叠在一起。然后使用view函数将数据进一步重新形状为N×L×C的形式,其中N表示样本数量,C表示数据的列数,L表示每天的小时数。最后使用transpose函数将数据重新排列为N×C×L的形式。

接下来,我们处理时间序列中的天气信息,该信息是有序的离散值。将天气信息转化为one-hot encoding,并将其与原始数据集拼接在一起,以便神经网络能够处理它。

最后我们提到了两种数据标准化的方式,这将有助于我们训练数据。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/812169.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

搭建网站 --- 快速WordPress个人博客并内网穿透发布到互联网

文章目录 快速WordPress个人博客并内网穿透发布到互联网 快速WordPress个人博客并内网穿透发布到互联网 我们能够通过cpolar完整的搭建起一个属于自己的网站,并且通过cpolar建立的数据隧道,从而让我们存放在本地电脑上的网站,能够为公众互联…

JS——输入输出语法数组的操作

JavaScript输入输出语法 目标:能写出常见的JavaScript输入输出语法 输出语法 语法1: document.write(要输出的内容)作用: 向body内输出内容 注意: 如果输出的内容写的是标签,也会被解析成网页元素 语法2&#xff1a…

2023大同首届信息技术产业峰会举行,共话数字经济新未来

7月28日,“聚势而强共领信创”2023大同首届信息技术产业峰会圆满举行。本次峰会由中共大同市委、大同市人民政府主办,中国高科技产业化研究会国际交流合作中心、山西省信创协会协办,中共大同市云冈区委、大同市云冈区人民政府、诚迈科技&…

一文深入了解Cmk

目录 一、Cmk(设备能力指数)介绍:二、Cmk(设备能力指数) 概念:三、Cmk的应用时机:四、Cmk前期准备和要求:五、Cmk测试要求:六、CMK计算公式:七、Cmk实际操作&…

机器学习的关键词和算法总结

随着全球各行业的数据治理、数字化转型智能化辅助的引入发展,机器学习(包括深度学习)在逐步深入到各行各业,所以,有必要对机器学习的常见术语,经典算法及应用场景进行一次总结,其实机器学习兴起…

Vue2 第九节 过滤器

(1)定义:对要显示的数据进行特定格式化后再显示 (2)语法: ① 注册过滤器 1)Vue.filter(name, callback) 全局过滤器 2) new Vue({filters:{}}) 局部过滤器 ② 使用过滤器 1&…

SQL注入漏洞及解决(PreparedStatement对象)

四、SQL注入漏洞 加个or true都能查出来,很危险!!! 会报错,因为代码中也拼了个引号 我们使用#将后面的内容注释起来,前面还是条完整的sql,还是能查到 使用—注释,代码可能加trim()&…

编译运行miniob最小数据库系统

minibo是一个用于教学的小型数据库系统,麻雀虽小五脏俱全,该项目包含了数据库的核心内容,并且代码量小,适合新手学习,最近由于需要学习c/cpp,因此打算从这个项目入手,本文就介绍编译运行miniob的…

《零基础入门学习Python》第072讲:GUI的终极选择:Tkinter9

这节课我们接着来讲解 Canvas 组件,既然 Cnavas 是画布的意思,那我们能不能让这个组件来设计一个画板呢?像Windows 自带的画图程序,我们的用户可以在上面随心所欲的绘制自己想要的图画,大家仔细想想,其实画…

美团小图币代挂教程

登陆美团官网获取对应的cookie 美团官网,点击右上角登陆对应账号。登陆成功后使用f12来获取 cookie 此时如果没有任何数据,点击网页刷新。找到如下的网络请求 赋值cookie项的全部内容,此时已经获取到对应账号的cookie 使用cookie登陆代挂…

NODEJS笔记

全局对象 global/window console.log/info/warn/error/time/timeEnd process.arch/platform/version/env/kill/pid/nextTick Buffer.alloc(5,abcde) String/toString setTimeout/clearTimeout setInterval/clearInterval setImmediate/clearImmediate process.nextTi…

1.netty介绍

1.介绍 是JBOSS通过的java开源框架是异步的,基于事件驱动(点击一个按钮调用某个函数)的网络应用框架,高性能高可靠的网络IO程序基于TCP,面向客户端高并发应用/点对点大量数据持续传输的应用是NIO框架 (IO的一层层封装) TCP/IP->javaIO和网络编程–>NIO—>Netty 2.应用…

HTTP请求走私漏洞简单分析

文章目录 HTTP请求走私漏洞的产生HTTP请求走私漏洞的分类HTTP请求走私攻击的危害确认HTTP请求走私漏洞通过时间延迟技术确认CL漏洞通过时间延迟技术寻找TE.CL漏洞 使用差异响应内容确认漏洞通过差异响应确认CL.TE漏洞通过差异响应确认TE.CL漏洞 请求走私漏洞的利用通过请求漏洞…

【面试题】与通义千问的芯片前端设计模拟面试归纳

这里是尼德兰的喵芯片设计相关文章,欢迎您的访问! 如果文章对您有所帮助,期待您的点赞收藏! 让我们一起为芯片前端全栈工程师而努力! 前言 两个小时,与chatGPT进行了一场数字IC前端设计岗的面试_尼德兰的喵的博客-CSDN博客 和GPT-3.5的回答可以对比品尝,味道更好。 模…

nacos源码打包及相关配置

nacos 本地下载后,需要 install 下: mvn clean install -Dmaven.test.skiptrue -Dcheckstyle.skiptrue -Dpmd.skiptrue -Drat.skiptruenacos源码修改后,重新打包生成压缩包命令:在 distribution 目录中运行: mvn -Pr…

数字化转型导师坚鹏:数字化时代扩大内需的8大具体建议

在日新月异的数字化时代、复杂多变的国际化环境下,扩大内需成为推动经济发展的国家战略,如何真正地扩大内需?结合本人15年的管理咨询经验及目前实际情况的深入研究,提出以下8大具体建议: 1、制定国民收入倍增计划。结…

QObject::connect: No such signal me::sendMsg(QString s) in ...

QObject::connect: No such signal me::sendMsg(QString s) in ... 解决方案 在使用qt4的connect中,爆的bug: 导致 teacher 的槽函数 receiveMsg(QString s) 一直没有被调用。。。。 解决方案 去掉参数名, 保留类型…

spring6——容器

文章目录 容器:IocIoc容器控制反转(Ioc)依赖注入IoC容器在Spring的实现 基于XML管理Bean搭建环境获取bean依赖注入setter注入构造器注入特殊值处理字面量赋值null值xml实体CDATA节 特殊类型属性注入为对象类型属性赋值方式一:引入…

Spring依赖注入和ioc在spring中的实现方式

目录 一、依赖注入 1.IOC思想 2.什么是依赖注入? 3.实例化对象中有pojo类型属性 二、IOC在Spring中的实现方式 1.获取bean的三种方式 1.1根据bean的id获取 1.2根据bean的类型获取(最常用,因为在IOC容器中,一个类型的bean只…

APUE学习62章终端(一): 整体概览

1. 什么是终端 <Linux_UNIX系统编程手册下>的第62.1介绍了整体概览&#xff0c;但是说得比较模糊&#xff0c;什么是终端这个问题请参考下面的博客: https://www.cnblogs.com/changrunwei/p/15759664.html 它讲解了以下概念: 终端(UNIX和Linux把人和机器可以交互的接…