-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdata_utils.py
More file actions
31 lines (27 loc) · 1.25 KB
/
Copy pathdata_utils.py
File metadata and controls
31 lines (27 loc) · 1.25 KB
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
from sklearn import preprocessing
import numpy as np
def normalize_data(df):
min_max_scaler = preprocessing.MinMaxScaler()
df['open'] = min_max_scaler.fit_transform(df.open.values.reshape(-1, 1))
df['high'] = min_max_scaler.fit_transform(df.high.values.reshape(-1, 1))
df['low'] = min_max_scaler.fit_transform(df.low.values.reshape(-1, 1))
df['volume'] = min_max_scaler.fit_transform(df.volume.values.reshape(-1, 1))
df['adj close'] = min_max_scaler.fit_transform(df['adj close'].values.reshape(-1, 1))
return df
def load_data(stock, seq_len):
amount_of_features = len(stock.columns)
data = stock.as_matrix()
sequence_length = seq_len + 1
result = []
for index in range(len(data) - sequence_length):
result.append(data[index: index + sequence_length])
result = np.array(result)
row = round(0.9 * result.shape[0])
train = result[:int(row), :]
x_train = train[:, :-1]
y_train = train[:, -1][:, -1]
x_test = result[int(row):, :-1]
y_test = result[int(row):, -1][:, -1]
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], amount_of_features))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], amount_of_features))
return [x_train, y_train, x_test, y_test]