Skip to content

Commit 5aa7b5a

Browse files
committed
adding inference method
1 parent f58c847 commit 5aa7b5a

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

stock_prediction_forecasting.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,32 @@
1414
# ==============================================================================
1515
import os
1616
from absl import app
17-
import pandas as pd
18-
from keras import Sequential
19-
from keras.layers import LSTM, Dropout, Dense
2017
import tensorflow as tf
21-
from keras.losses import mean_squared_error
2218
from sklearn.preprocessing import MinMaxScaler
23-
import datetime
19+
20+
from stock_prediction_numpy import StockData
21+
from datetime import date
2422
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
25-
import matplotlib.pyplot as plt
26-
import numpy as np
27-
import yfinance as yf
28-
import secrets
23+
2924

3025
def main(argv):
3126
print(tf.version.VERSION)
3227
inference_folder = os.path.join(os.getcwd(), 'GOOG_20200704_b5f47746c83698528343678663ac3c96')
3328

3429
# load future data
35-
36-
30+
data = StockData()
31+
min_max = MinMaxScaler(feature_range=(0, 1))
32+
x_test, y_test = data.generate_future_data(TIME_STEPS, min_max, date(2020, 7, 5), date(2021, 7, 5))
3733

34+
# load the weights from our best model
35+
model = tf.keras.models.load_model(os.path.join(inference_folder, 'model_weights.h5'))
36+
model.summary()
3837

38+
# perform a prediction
39+
test_predictions_baseline = model.predict(x_test)
40+
print(test_predictions_baseline)
3941

4042

4143
if __name__ == '__main__':
44+
TIME_STEPS = 60
4245
app.run(main)

stock_prediction_numpy.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,27 @@ def __daterange(self, start_date, end_date):
5757
for n in range(int((end_date - start_date).days)):
5858
yield start_date + timedelta(n)
5959

60-
def generate_future_data(self, start_date, end_date):
60+
def generate_future_data(self, time_steps, min_max, start_date, end_date):
6161
x_future = []
62+
y_future = []
6263
for single_date in self.__daterange(start_date, end_date):
6364
x_future.append(single_date)
65+
y_future.append(0.0)
66+
67+
test_data = pd.DataFrame({'Date': x_future, 'Close': y_future})
68+
test_data = test_data.set_index('Date')
69+
print(test_data)
70+
71+
test_scaled = min_max.fit_transform(test_data)
72+
x_test = []
73+
y_test = []
74+
for i in range(time_steps, test_scaled.shape[0]):
75+
x_test.append(test_scaled[i - time_steps:i])
76+
y_test.append(test_scaled[i, 0])
77+
78+
x_test, y_test = np.array(x_test), np.array(y_test)
79+
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
80+
return x_test, y_test
81+
82+
6483

0 commit comments

Comments
 (0)