-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_preprocessing.py
More file actions
31 lines (25 loc) · 915 Bytes
/
data_preprocessing.py
File metadata and controls
31 lines (25 loc) · 915 Bytes
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
import numpy as np
def build_s_a(sequence,n,m):
'''
Args:
sequence: Time series data
n: The number of historical data denoting the current state
m: The number of prediction steps in advance
Return:
state_mat: A matrix contains all states at each time step
best_action: The optimal action based on each state
'''
n_rows = len(sequence)-n-m+1
state_mat = np.zeros((n_rows,n))
best_action = np.zeros(n_rows)
for i in range(n_rows):
state_mat[i] = sequence[i:(i+n)]
best_action[i] = sequence[i+n+m-1]
return state_mat,best_action
def normalization(traindata,testdata):
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(traindata)
traindata_scaled = scaler.transform(traindata)
testdata_scaled = scaler.transform(testdata)
return traindata_scaled,testdata_scaled