LSTM時系列解析
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 |
import numpy import matplotlib.pyplot as plt from pandas import read_csv import math from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error #以下にコードを書いてください # データセットの作成 def create_dataset(dataset, look_back): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return numpy.array(dataX), numpy.array(dataY) # 乱数設定 numpy.random.seed(7) # データセットの読み込み dataframe = read_csv('nikkei225.csv', usecols=[1], engine='python', skipfooter=3) dataset = dataframe.values dataset = dataset.astype('float32') # 訓練データ、テストデータに分ける train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:] # データのスケーリング scaler = MinMaxScaler(feature_range=(0, 1)) scaler_train = scaler.fit(train) train = scaler_train.transform(train) test = scaler_train.transform(test) # データの作成 look_back = 10 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # データの整形 trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1)) testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1)) # LSTMモデルの作成と学習 model = Sequential() model.add(LSTM(64, return_sequences=True,input_shape=(look_back, 1))) model.add(LSTM(32)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(trainX, trainY, epochs=10, batch_size=1, verbose=2) # 予測データの作成 trainPredict = model.predict(trainX) testPredict = model.predict(testX) # スケールしたデータを元に戻す trainPredict = scaler_train.inverse_transform(trainPredict) trainY = scaler_train.inverse_transform([trainY]) testPredict = scaler_train.inverse_transform(testPredict) testY = scaler_train.inverse_transform([testY]) # 予測精度の計算 trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) # プロットのためのデータ整形 trainPredictPlot = numpy.empty_like(dataset) trainPredictPlot[:, :] = numpy.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict testPredictPlot = numpy.empty_like(dataset) testPredictPlot[:, :] = numpy.nan testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # テストデータのプロット plt.plot(dataframe[round(len(dataset)*0.67):]) plt.plot(testPredictPlot) plt.show() |
結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Epoch 1/10 44s - loss: 0.0040 Epoch 2/10 44s - loss: 0.0013 Epoch 3/10 43s - loss: 0.0011 Epoch 4/10 44s - loss: 7.8079e-04 Epoch 5/10 44s - loss: 5.9064e-04 Epoch 6/10 44s - loss: 5.5586e-04 Epoch 7/10 43s - loss: 5.2437e-04 Epoch 8/10 43s - loss: 5.4960e-04 Epoch 9/10 43s - loss: 5.3203e-04 Epoch 10/10 44s - loss: 4.9286e-04 Train Score: 270.96 RMSE Test Score: 144.13 RMSE |