def predict(self, stock): from model_handler import ModelHandler import matplotlib.dates as mdates import datetime as dt import numpy as np import matplotlib.pyplot as plt import pandas as pd myModelHandler = ModelHandler() regressor = myModelHandler.load_json_model(stock) # Importing the training set dataset = pd.read_csv(stock.csv_name) # dates = dataset.iloc[len(dataset) - 31:len(dataset) - 1, 0].values dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates] # Feature Scaling from sklearn.preprocessing import MinMaxScaler sc = MinMaxScaler(feature_range=(0, 1)) dataset_test = dataset[len(dataset) - 30:] real_stock_price = dataset_test.iloc[:, 1:2].values # Getting the predicted stock price dataset = dataset['Open'] inputs = dataset[len(dataset) - len(dataset_test) - 60:].values inputs = inputs.reshape(-1, 1) inputs = sc.fit_transform(inputs) X_test = [] for i in range(60, 90): X_test.append(inputs[i - 60:i, 0]) X_test = np.array(X_test) X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) predicted_stock_price = regressor.predict(X_test) print(real_stock_price) predicted_stock_price = sc.inverse_transform(predicted_stock_price) plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) plt.plot(dates, real_stock_price, color='red', label=f'Actual {stock.ticker} Stock Price') plt.plot(dates, predicted_stock_price, color='blue', label=f'Predicted {stock.ticker} Stock Price') plt.gcf().autofmt_xdate() plt.title(f'{stock.ticker} Stock Price Prediction') plt.xlabel('Time') plt.ylabel(f'{stock.ticker} Stock Price') plt.legend() plt.show()
def forecast(self, stock): """Loads the trained model, forecasts the values then displays a graph of the values""" # Load the trained model model_handler = ModelHandler() model = model_handler.load_json_model(stock) # Importing the training set dataset = pd.read_csv(stock.csv_name) dates = dataset.iloc[len(dataset) - 31:len(dataset) - 1, 0].values dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates] # Create the test dataset dataset_test = dataset[len(dataset) - 30:] real_stock_price = dataset_test.iloc[:, 1:2].values dataset = dataset['Open'] inputs = dataset[len(dataset) - len(dataset_test) - 60:].values inputs = inputs.reshape(-1, 1) # Feature Scaling sc = MinMaxScaler(feature_range=(0, 1)) inputs = sc.fit_transform(inputs) x_test = [] x_test.append(inputs[0:60, 0]) predicted_values = [] for i in range(1, 31): x_test_np = np.array(x_test) x_test_np = np.reshape(x_test_np, (x_test_np.shape[0], x_test_np.shape[1], 1)) new_data = model.predict(x_test_np) predicted_values.append(new_data[0]) x_test[0] = np.delete(x_test[0], 0) x_test[0] = np.concatenate([x_test[0], new_data[0]]) predicted_values = sc.inverse_transform(predicted_values) plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) plt.plot(dates, real_stock_price, color='red', label=f'Actual {stock.ticker} Stock Price') plt.plot(dates, predicted_values, color='blue', label=f'Predicted {stock.ticker} Stock Price') plt.gcf().autofmt_xdate() plt.title(f'{stock.ticker} Stock Price Prediction') plt.xlabel('Time') plt.ylabel(f'{stock.ticker} Stock Price') plt.legend() plt.show()