Sunday, September 24, 2017

Prediction using trained model and parameters

Caution: This code doesn't work under CUI operation system, which means you can not run this program with virtual box + vagrant (at least without GUI environment).
Disclaimer: This code aims for sharing code examples publicly. Even if you get any loss using this code, we do not take any responsibility.

This code needs stock price data. Create a folder for the data as "csv" in your directory.

In the csv folder, put these data files. These are price data of Japanese stock market.
Download: https://github.com/shunakanishi/japanese_stockprice

Go back to the first directory from CSV folder. Create a python script as "stockprice.py".

We will prepare trained model and parameters in "model" -> "stockprice".

In the "model" folder:

In the "stockprice" folder:

If you don't have trained model and parameters, go to this post and create trained model and parameters at first.

Write the following inside "stockprice.py":
#-*- coding: utf-8 -*-
import numpy
import pandas
import matplotlib.pyplot as plt

from sklearn import preprocessing
from keras.models import Sequential
from keras.models import model_from_json
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
import keras.backend.tensorflow_backend as KTF
import os.path

class Prediction :

  def __init__(self):
    self.length_of_sequences = 10
    self.in_out_neurons = 1
    self.hidden_neurons = 300


  def load_data(self, data, n_prev=10):
    X, Y = [], []
    for i in range(len(data) - n_prev):
      X.append(data.iloc[i:(i+n_prev)].as_matrix())
      Y.append(data.iloc[i+n_prev].as_matrix())
    retX = numpy.array(X)
    retY = numpy.array(Y)
    return retX, retY


  def create_model(self, f_model, model_filename, weights_filename) :
    print(os.path.join(f_model,model_filename))
    if os.path.isfile(os.path.join(f_model,model_filename)):
      print('Saved parameters found. I will use this file...')
      json_string = open(os.path.join(f_model, model_filename)).read()
      model = model_from_json(json_string)
      model.summary()
      model.compile(loss="mape", optimizer="adam")
      model.load_weights(os.path.join(f_model,weights_filename))
    else:
      print('Saved parameters Not found. Please prepare model and parameters.')
      model = None
    return model

if __name__ == "__main__":

  f_log = './log'
  f_model = './model/stockprice'
  model_filename = 'stockprice_model.json'
  yaml_filename = 'stockprice_model.yaml'
  weights_filename = 'stockprice_model_weights.hdf5'

  prediction = Prediction()

  # Data
  data = None
  for year in range(2007, 2017):
    data_ = pandas.read_csv('csv/indices_I101_1d_' + str(year) +  '.csv')
    data = data_ if (data is None) else pandas.concat([data, data_])
  data.columns = ['date', 'open', 'high', 'low', 'close']
  data['date'] = pandas.to_datetime(data['date'], format='%Y-%m-%d')
  # Data of closing price
  data['close'] = preprocessing.scale(data['close'])
  data = data.sort_values(by='date')
  data = data.reset_index(drop=True)
  data = data.loc[:, ['date', 'close']]

  # 100% of the data is used as test data.
  # split_pos = int(len(data) * 0.8)
  # x_train, y_train = prediction.load_data(data[['close']].iloc[0:split_pos], prediction.length_of_sequences)
  x_test,  y_test  = prediction.load_data(data[['close']], prediction.length_of_sequences)

  old_session = KTF.get_session()

  model = prediction.create_model(f_model, model_filename, weights_filename)

  predicted = model.predict(x_test)
  result = pandas.DataFrame(predicted)
  result.columns = ['predict']
  result['actual'] = y_test
  result.plot()
  plt.show()

And do this command:
$ sudo python3 stockprice.py


100% of the data will be used for the test.