(2) 用张量表示实际数据---Time series

Real-world data representation with tensors

NO. 2 Time series
  1. In the meantime, we’ll switch to another interesting data set: data from a Washington, D.C., bike sharing system reporting the hourly count of rental bikes between 2011 and 2012 in the Capital bike-share system with the corresponding weather and seasonal information. The goal is to take a flat 2D data set and transform it into a 3D one. Dataset
    1
    处理数据:
import torch
import numpy as np
path = "E:/Deep Learning and PyTorch/No2 Deep Learning with PyTorch/dlwpt-code-master/data/p1ch4/bike-sharing-dataset/hour-fixed.csv"
bike_np = np.loadtxt(path, 
                     dtype=np.float32, 
                     delimiter=",", 
                     skiprows=1, 
                     converters={1: lambda x: float(x[8:10])})
bikes = torch.from_numpy(bike_np)
bikes >>> tensor([[1.0000e+00, 1.0000e+00, 1.0000e+00,  ..., 3.0000e+00, 1.3000e+01,
			         1.6000e+01],
			        [2.0000e+00, 1.0000e+00, 1.0000e+00,  ..., 8.0000e+00, 3.2000e+01,
			         4.0000e+01],
			        [3.0000e+00, 1.0000e+00, 1.0000e+00,  ..., 5.0000e+00, 2.7000e+01,
			         3.2000e+01],
			        ...,
			        [1.7377e+04, 3.1000e+01, 1.0000e+00,  ..., 7.0000e+00, 8.3000e+01,
			         9.0000e+01],
			        [1.7378e+04, 3.1000e+01, 1.0000e+00,  ..., 1.3000e+01, 4.8000e+01,
			         6.1000e+01],
			        [1.7379e+04, 3.1000e+01, 1.0000e+00,  ..., 1.2000e+01, 3.7000e+01,
			         4.9000e+01]])
bikes.shape >>> torch.Size([17520, 17])
daily_bikes = bikes.view(-1, 24, bikes.shape[1])
daily_bikes.shape >>> torch.Size([730, 24, 17])		      

将数据处理,得到3维数据,维度为[730, 24, 17]

-----------------------------------------

Tip: 关于view的用法:

test = torch.tensor([[1,2,3,4,5,6],
                     [6,7,8,9,9,0],
                     [8,5,6,8,9,2],
                     [5,6,8,9,0,0],
                     [2,3,5,7,8,9],
                     [6,7,9,0,6,4]])
test.shape >>> torch.Size([6, 6])
a = test.view(-1, 3, 6)
a.shape, a >>>
(torch.Size([2, 3, 6]),
 tensor([[[1, 2, 3, 4, 5, 6],
          [6, 7, 8, 9, 9, 0],
          [8, 5, 6, 8, 9, 2]],
 
         [[5, 6, 8, 9, 0, 0],
          [2, 3, 5, 7, 8, 9],
          [6, 7, 9, 0, 6, 4]]]))
-----------------------------------------

转置

daily_bikes = daily_bikes.transpose(1,2)
daily_bikes.shape >>> torch.Size([730, 17, 24])

编码

# 新容器创建
daily_weather_onehot = torch.zeros(daily_bikes.shape[0], 4, daily_bikes.shape[2])
daily_weather_onehot.shape >>> torch.Size([730, 4, 24])
# 对编码的数进行处理
index_daily = daily_bikes[:,9,:]
index_daily.shape >>> torch.Size([730, 24])
index_daily = index_daily.long().unsqueeze(1)-1
index_daily.shape >>> torch.Size([730, 1, 24])
# 重新编码
daily_weather_onehot = daily_weather_onehot.scatter_(1, index_daily, 1)
daily_weather_onehot.shape >>> torch.Size([730, 4, 24])
# 合并
daily_bikes = torch.cat((daily_bikes, daily_weather_onehot), dim=1)
daily_bikes.shape >>> torch.Size([730, 21, 24])

接着进行数的归一化,标准化

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值