-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_data.py
90 lines (69 loc) · 2.21 KB
/
get_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding:utf-8 -*-
"""
@Time: 2022/03/02 11:20
@Author: KI
@File: get_data.py
@Motto: Hungry And Humble
"""
import sys
import numpy as np
import pandas as pd
import torch
sys.path.append('../')
from torch.utils.data import Dataset, DataLoader
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
clients_wind = ['Task1_W_Zone' + str(i) for i in range(1, 11)]
def load_data(file_name):
df = pd.read_csv('data/Wind/Task 1/Task1_W_Zone1_10/' + file_name + '.csv', encoding='gbk')
columns = df.columns
df.fillna(df.mean(), inplace=True)
for i in range(3, 7):
MAX = np.max(df[columns[i]])
MIN = np.min(df[columns[i]])
df[columns[i]] = (df[columns[i]] - MIN) / (MAX - MIN)
return df
class MyDataset(Dataset):
def __init__(self, data):
self.data = data
def __getitem__(self, item):
return self.data[item]
def __len__(self):
return len(self.data)
def nn_seq_wind(file_name, B):
print('data processing...')
dataset = load_data(file_name)
# split
train = dataset[:int(len(dataset) * 0.6)]
val = dataset[int(len(dataset) * 0.6):int(len(dataset) * 0.8)]
test = dataset[int(len(dataset) * 0.8):len(dataset)]
def process(data):
columns = data.columns
wind = data[columns[2]]
wind = wind.tolist()
data = data.values.tolist()
seq = []
for i in range(len(data) - 30):
train_seq = []
train_label = []
for j in range(i, i + 24):
train_seq.append(wind[j])
for c in range(3, 7):
train_seq.append(data[i + 24][c])
train_label.append(wind[i + 24])
train_seq = torch.FloatTensor(train_seq).view(-1)
train_label = torch.FloatTensor(train_label).view(-1)
seq.append((train_seq, train_label))
seq = MyDataset(seq)
seq = DataLoader(dataset=seq, batch_size=B, shuffle=False, num_workers=0)
return seq
Dtr = process(train)
Val = process(val)
Dte = process(test)
return Dtr, Val, Dte
def get_mape(x, y):
"""
:param x: true value
:param y: pred value
:return: mape
"""
return np.mean(np.abs((x - y) / x))