-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprocessing.py
More file actions
106 lines (83 loc) · 2.83 KB
/
processing.py
File metadata and controls
106 lines (83 loc) · 2.83 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""
Created on Tue May 17 15:37:40 2016
@author: Alex
"""
import numpy as np
from sklearn import preprocessing
from sklearn.metrics import mean_squared_error, classification_report
import matplotlib.pylab as plt
import datetime as dt
import time
def load_snp_returns():
f = open('table.csv', 'rb').readlines()[1:]
raw_data = []
raw_dates = []
for line in f:
try:
open_price = float(line.split(',')[1])
close_price = float(line.split(',')[4])
raw_data.append(close_price - open_price)
raw_dates.append(line.split(',')[0])
except:
continue
return raw_data[::-1], raw_dates[::-1]
def load_snp_close():
f = open('table.csv', 'rb').readlines()[1:]
raw_data = []
raw_dates = []
for line in f:
try:
close_price = float(line.split(',')[4])
raw_data.append(close_price)
raw_dates.append(line.split(',')[0])
except:
continue
return raw_data, raw_dates
def split_into_chunks(data, train, predict, step, binary=True, scale=True):
X, Y = [], []
for i in range(0, len(data), step):
try:
x_i = data[i:i+train]
y_i = data[i+train+predict]
# Use it only for daily return time series
if binary:
if y_i > 0.:
y_i = [1., 0.]
else:
y_i = [0., 1.]
if scale: x_i = preprocessing.scale(x_i)
else:
timeseries = np.array(data[i:i+train+predict])
if scale: timeseries = preprocessing.scale(timeseries)
x_i = timeseries[:-1]
y_i = timeseries[-1]
except:
break
X.append(x_i)
Y.append(y_i)
return X, Y
def shuffle_in_unison(a, b):
# courtsey http://stackoverflow.com/users/190280/josh-bleecher-snyder
assert len(a) == len(b)
shuffled_a = np.empty(a.shape, dtype=a.dtype)
shuffled_b = np.empty(b.shape, dtype=b.dtype)
permutation = np.random.permutation(len(a))
for old_index, new_index in enumerate(permutation):
shuffled_a[new_index] = a[old_index]
shuffled_b[new_index] = b[old_index]
return shuffled_a, shuffled_b
def create_Xt_Yt(X, y, percentage=0.8):
X_train = X[0:len(X) * percentage]
Y_train = y[0:len(y) * percentage]
X_train, Y_train = shuffle_in_unison(X_train, Y_train)
X_test = X[len(X) * percentage:]
Y_test = y[len(X) * percentage:]
return X_train, X_test, Y_train, Y_test
def createnumpyarray(X,Y,trainsize):
X_data = np.zeros((len(X),1, trainsize))
Y_data = np.zeros((len(Y),1,trainsize))
for i in range(len(X)):
X_data[i,0]= X[i]
Y_data[i,0]=Y[i]
return X_data , Y_data