LSTM (Long short-term memory) は、RNN (Recurrent Neural Network) の拡張で、時系列データ (Sequential Data)に対するモデル

lstm.py 

#
#import pdb; pdb.set_trace()

import sys
import numpy as np
import pandas as pd

length_of_sequences = 5

def convert_data(values):
   returns = pd.Series(values).pct_change()
   #returns = pd.DataFrame(values).pct_change()
   ret_index = (1 + returns).cumprod()
   ret_index[0] = 1.0
   return ret_index

def create_train_data(values,samples):
   train_x = []
   train_y = []

   for i in np.arange(0, (len(values) - samples - 1)):
      s = i + samples
      feature = values.iloc[i:s]
      if feature[s - 1] < values[s]:
      #if feature.loc[s - 1] < values.loc[s]:
         train_y.append([1])    # up
      else:
         train_y.append([0])    # down
      train_x.append([[x] for x in feature.values])

   return np.array(train_x),np.array(train_y)

file_name = 'stock_yasukawa.csv'
#adj_end = pd.read_csv(file_name,usecols=[6])
adj_end = pd.read_csv(file_name)
adj_end = adj_end['5']
adj_end = convert_data(adj_end)
#print adj_end

X,Y = create_train_data(adj_end,length_of_sequences)

split_pos = int(len(X) * 0.8)
train_x = X[:split_pos]
train_y = Y[:split_pos]
test_x = X[:split_pos]
test_y = Y[:split_pos]


# keras/tensorflow で、LSTM
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM

hidden_neurons = 128
in_out_neurons = 1

def create_model():
   model = Sequential()
   model.add(LSTM(hidden_neurons,batch_input_shape=(None,length_of_sequences,in_out_n
eurons)))
   model.add(Dropout(0.5))
   model.add(Dense(in_out_neurons))
   model.add(Activation("sigmoid"))

   return model

model = create_model()
model.compile(loss="binary_crossentropy",optimizer="adam",metrics=['accuracy'])
history = model.fit(train_x,train_y,batch_size=10,verbose=1)

# evaluation

score = model.evaluate(test_x,test_y,batch_size=10,verbose=1)
print("score: ",score)

preds = model.predict(test_x)
correct = 0
for i in range(len(preds)):
   p = 0 if preds[i][0] < 0.5 else 1
   t = test_y[i][0]
   if p == t:
      correct += 1

print("correct rate: ",correct/len(preds))




計算できたのだが、期待するような数字にはもちろんならない。世の中そんなに甘くないことを再び確認する。

◆ネタ元
LSTM で株価予測してみる