超簡単ディープラーニングで株価予測(Python・Keras・ローソク足チャート画像利用)
PythonでKeras(ディープラーニング)使って超簡単に株価予想(ローソク足チャート画像利用)
1. 前準備
下記フォルダ構成で、画像を準備。upには翌日株価が上がった、downには下がった画像。trainに(up、down共に)4000枚、validationに(up、down共に)500枚用意
chart
├── train
│ ├── down
│ │ └── XXX.png
│ └── up
│ │ └── XXX.png
└── validation
├── down
│ └── XXX.png
└── up
└── XXX.png
用意したローソク足チャート画像
*ローソク足チャート画像の作り方はこちらへ
2. インストール
pip install tensorflow keras matplotlib Pillow
3. ファイル作成
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import os
base_dir = 'chart'
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(124, 92, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(124, 92),
batch_size=20,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(124, 92),
batch_size=20,
class_mode='binary')
for data_batch, labels_batch in train_generator:
print('data batch shape:', data_batch.shape)
print('labels batch shape:', labels_batch.shape)
break
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=30,
validation_data=validation_generator,
validation_steps=50)
model.save('chart.h5')
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
fig = plt.figure()
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.savefig('accuracy.png')
plt.close()
fig = plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.savefig('loss.png')
plt.close()
4. 実行
$ python chart.py
Epoch 1/30
100/100 [==============================] - 39s 386ms/step - loss: 0.6955 - acc: 0.4945 - val_loss: 0.7012 - val_acc: 0.5000
Epoch 2/30
100/100 [==============================] - 35s 345ms/step - loss: 0.6941 - acc: 0.5010 - val_loss: 0.6852 - val_acc: 0.5000
Epoch 3/30
100/100 [==============================] - 34s 341ms/step - loss: 0.6944 - acc: 0.4945 - val_loss: 0.6926 - val_acc: 0.5140
Epoch 4/30
100/100 [==============================] - 36s 356ms/step - loss: 0.6934 - acc: 0.5075 - val_loss: 0.6905 - val_acc: 0.5000
Epoch 5/30
100/100 [==============================] - 33s 334ms/step - loss: 0.6934 - acc: 0.5000 - val_loss: 0.6938 - val_acc: 0.4990
:
Epoch 25/30
100/100 [==============================] - 35s 351ms/step - loss: 0.6546 - acc: 0.6180 - val_loss: 0.6664 - val_acc: 0.5370
Epoch 26/30
100/100 [==============================] - 35s 353ms/step - loss: 0.6578 - acc: 0.6050 - val_loss: 0.7326 - val_acc: 0.5290
Epoch 27/30
100/100 [==============================] - 34s 344ms/step - loss: 0.6599 - acc: 0.6055 - val_loss: 0.6740 - val_acc: 0.5650
Epoch 28/30
100/100 [==============================] - 35s 346ms/step - loss: 0.6573 - acc: 0.6085 - val_loss: 0.6692 - val_acc: 0.5550
Epoch 29/30
100/100 [==============================] - 34s 339ms/step - loss: 0.6436 - acc: 0.6290 - val_loss: 0.7097 - val_acc: 0.5390
Epoch 30/30
100/100 [==============================] - 36s 364ms/step - loss: 0.6413 - acc: 0.6375 - val_loss: 0.8104 - val_acc: 0.5590
以上、結果考えなければ超簡単!