-
Notifications
You must be signed in to change notification settings - Fork 4
/
StockPrediction_onlineML.py
158 lines (111 loc) · 4.54 KB
/
StockPrediction_onlineML.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import tensorflow as tf
import matplotlib as mplt
mplt.use('agg') # Must be before importing matplotlib.pyplot or pylab!
import matplotlib.pyplot as plt
import numpy as np
import random
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.ensemble import RandomForestRegressor
from math import sqrt
from sklearn.linear_model import LinearRegression
from sklearn import linear_model
class RNNConfig():
input_size = 1
num_steps = 2
lstm_size = 128
num_layers = 1
keep_prob = 0.8
batch_size = 64
init_learning_rate = 0.001
learning_rate_decay = 0.99
init_epoch = 3 # 5
max_epoch = 30 # 100 or 50
features = 2
test_ratio = 0.2
fileName = 'AIG.csv'
graph = tf.Graph()
column1_min = 10
column1_max = 2000
column2_min = 0
column2_max = 50000000
column1 = 'Close'
column2 = 'Volume'
config = RNNConfig()
def segmentation(data):
seq = [price for tup in data[[config.column2, config.column1]].values for price in tup]
seq = np.array(seq)
# split into items of features
seq = [np.array(seq[i * config.features: (i + 1) * config.features])
for i in range(len(seq) // config.features)]
# split into groups of num_steps
temp_X = np.array([seq[i: i + config.num_steps] for i in range(len(seq) - config.num_steps)])
X = []
for dataslice in temp_X:
temp = dataslice.flatten()
X.append(temp)
X = np.asarray(X)
y = np.array([seq[i + config.num_steps] for i in range(len(seq) - config.num_steps)])
# get only close values
y = [y[i][1] for i in range(len(y))]
y = np.asarray(y)
return X, y
def scale(data):
data[config.column1] = (data[config.column1] - config.column1_min) / (config.column1_max - config.column1_min)
data[config.column2] = (data[config.column2] - config.column2_min) / (config.column2_max - config.column2_min)
return data
def pre_process():
stock_data = pd.read_csv(config.fileName)
stock_data = stock_data.reindex(index=stock_data.index[::-1])
# ---for segmenting original data ---------------------------------
original_data = pd.read_csv(config.fileName)
original_data = original_data.reindex(index=original_data.index[::-1])
train_size = int(len(stock_data) * (1.0 - config.test_ratio))
train_data = stock_data[:train_size]
test_data = stock_data[train_size:]
original_data = original_data[train_size:]
# -------------- processing train data---------------------------------------
scaled_train_data = scale(train_data)
train_X, train_y = segmentation(scaled_train_data)
# -------------- processing test data---------------------------------------
scaled_test_data = scale(test_data)
test_X, test_y = segmentation(scaled_test_data)
# ----segmenting original test data-----------------------------------------------
nonescaled_X, nonescaled_y = segmentation(original_data)
return train_X, train_y, test_X, test_y, nonescaled_y
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
def plot(true_vals,pred_vals,name):
days = range(len(true_vals))
plt.plot(days, true_vals, label='truth close')
plt.plot(days, pred_vals, label='pred close')
plt.legend(loc='upper left', frameon=False)
plt.xlabel("day")
plt.ylabel("closing price")
plt.grid(ls='--')
plt.savefig(name, format='png', bbox_inches='tight', transparent=False)
plt.close()
def get_scores(name,pred_vals,nonescaled_y):
print(name)
meanSquaredError = mean_squared_error(nonescaled_y, pred_vals)
rootMeanSquaredError = sqrt(meanSquaredError)
print("RMSE:", rootMeanSquaredError)
mae = mean_absolute_error(nonescaled_y, pred_vals)
print("MAE:", mae)
mape = mean_absolute_percentage_error(nonescaled_y, pred_vals)
print("MAPE:", mape)
def SGD():
train_X, train_y, test_X, test_y, nonescaled_y = pre_process()
clf = linear_model.SGDRegressor()
for i in range(len(train_X)):
X, y = train_X[i:i + 1], train_y[i:i + 1]
clf.partial_fit(X, y)
predsgdr = clf.predict(test_X)
pred_vals = [(pred * (config.column1_max - config.column1_min)) + config.column1_min for pred in predsgdr]
pred_vals = np.asarray(pred_vals)
get_scores("---------SGDRegressor----------", pred_vals, nonescaled_y)
plot(nonescaled_y, pred_vals, "SGDRegressor Prediction Vs Truth.png")
if __name__ == '__main__':
SGD()