-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_model.py
70 lines (57 loc) · 2.5 KB
/
data_model.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
import numpy as np
import os
import pandas as pd
import random
import time
random.seed(time.time())
class StockDataSet(object):
def __init__(self,
stock_sym,
input_size=1,
num_steps=30,
test_ratio=0.1,
normalized=True,
close_price_only=True):
self.stock_sym = stock_sym
self.input_size = input_size
self.num_steps = num_steps
self.test_ratio = test_ratio
self.close_price_only = close_price_only
self.normalized = normalized
# Read csv file
raw_df = pd.read_csv(os.path.join("data", "%s.csv" % stock_sym))
# Merge into one sequence
if close_price_only:
self.raw_seq = raw_df['Close'].tolist()
else:
self.raw_seq = [price for tup in raw_df[['Open', 'Close']].values for price in tup]
self.raw_seq = np.array(self.raw_seq)
self.train_X, self.train_y, self.test_X, self.test_y = self._prepare_data(self.raw_seq)
def info(self):
return "StockDataSet [%s] train: %d test: %d" % (
self.stock_sym, len(self.train_X), len(self.test_y))
def _prepare_data(self, seq):
# split into items of input_size
seq = [np.array(seq[i * self.input_size: (i + 1) * self.input_size])
for i in range(len(seq) // self.input_size)]
if self.normalized:
seq = [seq[0] / seq[0][0] - 1.0] + [
curr / seq[i][-1] - 1.0 for i, curr in enumerate(seq[1:])]
# split into groups of num_steps
X = np.array([seq[i: i + self.num_steps] for i in range(len(seq) - self.num_steps)])
y = np.array([seq[i + self.num_steps] for i in range(len(seq) - self.num_steps)])
train_size = int(len(X) * (1.0 - self.test_ratio))
train_X, test_X = X[:train_size], X[train_size:]
train_y, test_y = y[:train_size], y[train_size:]
return train_X, train_y, test_X, test_y
def generate_one_epoch(self, batch_size):
num_batches = int(len(self.train_X)) // batch_size
if batch_size * num_batches < len(self.train_X):
num_batches += 1
batch_indices = range(num_batches)
random.shuffle(batch_indices)
for j in batch_indices:
batch_X = self.train_X[j * batch_size: (j + 1) * batch_size]
batch_y = self.train_y[j * batch_size: (j + 1) * batch_size]
assert set(map(len, batch_X)) == {self.num_steps}
yield batch_X, batch_y