Real-world data representation with tensors
NO. 2 Time series
- 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
处理数据:
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])
接着进行数的归一化,标准化