-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpredictions.py
119 lines (88 loc) · 3.85 KB
/
predictions.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
# Standard packages
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
sns.set_style('whitegrid')
plt.style.use("fivethirtyeight")
import os
os.environ['KMP_WARNINGS'] = 'off'
from sklearn.preprocessing import MinMaxScaler
class Predict(object):
def __init__(self):
pass
def train(self, train_data, model, show_progress = True):
'''
Input: train_data - list of input values (numpy array) and target values
(numpy array) of training data
model - model to be trained
show_progress - if the training process is showed (boolean)
'''
self.x_train = train_data[0]
self.y_train = train_data[1]
history = model.fit(self.x_train, self.y_train, batch_size = 1, epochs = 1)
self.model = model
# if show_progress:
# plt.plot(history.history['loss'])
# plt.title('RMS loss')
# plt.ylabel('$\mathcal{L}$')
# plt.xlabel('epoch')
# plt.grid('on')
# plt.show()
def predict(self, test_data, scaler, data_scaled = True, show_predictions = True):
'''
Input: test_data - list of input values (numpy array) and target values
(numpy array) of validation data
scaler - scaler object to inversely scale predictions
data_scaled - if scaler were used in the preprocessing (boolean)
Output: predictions - numpy array of the predicted values
'''
try:
model = self.model
except Exception as e:
print('Unable to load model, train the model before predicting!')
return None
self.x_test = test_data[0]
self.y_test = test_data[1]
predictions = model.predict(self.x_test)
if data_scaled:
predictions = scaler.inverse_transform(predictions)
#rmse = np.sqrt(np.mean(((predictions - self.y_test) ** 2)))
return predictions
def plot_predictions(df, train_data_size, predictions):
'''
Input: df - dataframe of stock values
train_data_size - length of the training data, number of elements (int)
predictions - numpy array of the prdicted values
'''
colors = ['#579BF5', '#C694F6', '#F168F1']
fig = go.Figure()
train = df[:train_data_size]
valid = df[train_data_size:]
valid['Predictions'] = predictions
x_train = [str(train.index[i]).split()[0] for i in range(len(train))]
x_val = [str(valid.index[i]).split()[0] for i in range(len(valid))]
fig.add_trace(
go.Scatter(x=x_train, y=train['Adj Close'], mode='lines', line_color=colors[0], line_width=3,
name='Training data'))
fig.add_trace(
go.Scatter(x=x_val, y=valid['Adj Close'], mode='lines', line_color=colors[1], line_width=3,
name='Validation data'))
fig.add_trace(
go.Scatter(x=x_val, y=valid['Predictions'], mode='lines', line_color=colors[2], line_width=3,
name='Predictions'))
fig.update_layout(showlegend=True)
fig.update_layout(title=dict(text=f'Predictions of stock "{train["Company stock name"][0]}" from {x_val[0]} to {x_val[len(valid) - 1]}',
xanchor='auto'),
xaxis=go.layout.XAxis(
title=go.layout.xaxis.Title(
text="Date")),
yaxis=go.layout.YAxis(
title=go.layout.yaxis.Title(
text="Adjusted closing price USD ($)"))
)
fig.show()