컴퓨터/Python

[Python] 파이썬, 딥러닝 CNN을 이용한 주식 가격 예측(3)

sjblog 2022. 9. 29. 12:42
반응형

1. 전체 과정 요약

데이터 준비, 학습, 예측의 순서로 총 3번의 글을 작성하였습니다.

[Python] 파이썬, 딥러닝 CNN을 이용한 주식 가격 예측(1)
https://sjblog1.tistory.com/65

 

[Python] 파이썬, 딥러닝 CNN을 이용한 주식 가격 예측(1)

결론적으로, 본인은 CNN을 통한 주식 가격의 예측에 실패하였습니다. Epochs, Batch size 등 과 같은 Hyperparameter (사용자가 입맛(?)대로 설정하는)를 변경하시거나, CNN을 활용한 다른 주식 가격 예측 전

sjblog1.tistory.com


[Python] 파이썬, 딥러닝 CNN을 이용한 주식 가격 예측(2)
https://sjblog1.tistory.com/66

 

[Python] 파이썬, 딥러닝 CNN을 이용한 주식 가격 예측(2)

1. 전체 과정 요약 데이터 준비, 학습, 예측의 순서로 총 3번의 글을 작성하였습니다. [Python] 파이썬, 딥러닝 CNN을 이용한 주식 가격 예측(1) https://sjblog1.tistory.com/65 [Python] 파이썬, 딥러닝 CNN을..

sjblog1.tistory.com

 

2. 패키지 추천 버전

import tensorflow as tf
from skimage.segmentation import mark_boundaries
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
import cv2
import copy
from lime import lime_image

skimage는

pip install scikit-image


그리고, lime 패키지를 이용하여 이미지의 어느 영역이 예측 결과에 영향을 미쳤는지 확인해보겠습니다.

3. 출력 색상 변경

formatters = {
    'Red': '\033[91m',
    'Green': '\033[92m',
    'Blue': '\033[94m',
    'END': '\033[0m'
}

def printlog(content, color):
    if color == 'Green':
        print('{Green}'.format(**formatters) + datetime.now().strftime('[%m/%d %H:%M:%S] ') + str(content) + '{END}'.format(**formatters))
    elif color == 'Blue':
        print('{Blue}'.format(**formatters) + datetime.now().strftime('[%m/%d %H:%M:%S] ') + str(content) + '{END}'.format(**formatters))
    else:
        print('{Red}'.format(**formatters) + datetime.now().strftime('[%m/%d %H:%M:%S] ') + str(content) + '{END}'.format(**formatters))

 

4. 변수 설정

img_height, img_width = 150, 150
img_path = 'test_image.jpeg'

모델에 사용한 img_height, img_width를 설정해주세요.

예측에 사용할 이미지를 설정해주세요. (이미지는 현재 폴더에 넣어주셔야 합니다.)

5. 이미지 가져오기

test_img = tf.keras.utils.load_img(path=img_path, color_mode='rgb')
test_img = tf.keras.utils.img_to_array(img=test_img, data_format='channels_last')
test_img = cv2.resize(test_img, dsize=[img_height,img_width], interpolation=cv2.INTER_NEAREST)
test_img = test_img.astype(np.uint8)
ref_img = copy.deepcopy(x=test_img)
temp_img = np.concatenate((test_img, test_img, test_img), axis=-1)

test_img = test_img / 255.
print('external image(s) shape:', test_img.shape)

# load_model
model = tf.keras.models.load_model('Results/CNN.h5')

이미지를 불러오고, 정규화 및 shape를 확인합니다.

사용할 모델은 이전에 저장한 Results/CNN.h5 입니다.

6. 이미지 예측

def generate_explanation(model, input_image):
    explainer = lime_image.LimeImageExplainer()
    explanation = explainer.explain_instance(image=input_image, classifier_fn=model.predict, hide_color=0,
                                             num_samples=1000, random_seed=18)
    temp, mask = explanation.get_image_and_mask(label=explanation.top_labels[0], positive_only=True, num_features=5,
                                                hide_rest=False)
    return temp, mask



이미지에 적용할 lime 함수를 만들어줍니다.

# binary classification 역치
threshold = 0.5

test_img_reshape=np.expand_dims(test_img, axis=0)
score = model.predict(test_img_reshape)    # 확률
Y_pred = np.where(score > threshold, 1,0)      # 예측 결과

temp, mask = generate_explanation(model=model, input_image=test_img)

printlog('Prediction: ' + str(Y_pred) + ', probablity: ' + str(score), 'Green')

p1 = plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(mark_boundaries(temp, mask))
plt.axis('off')
plt.suptitle('Prediction: ' + str(Y_pred) + ', probablity: ' + str(score))

plt.subplot(1, 2, 2)
plt.imshow(ref_img.squeeze())
plt.axis('off')
plt.tight_layout()
p1.savefig(fname='Results/predict.png')
plt.show()

실제 예측 결과와 예측 결과가 맞을 확률을 표시하였습니다.

또한 lime을 이용하여 어느 부분이 예측 결과에 영향을 미쳤는지 확인할 수 있습니다.

결과는 Results/predict.png로 저장합니다.

7. 마무리

주식 데이터를 활용한 CNN 예측은 실패하였습니다.

Garbage in, garbage out (GIGO)

쓰레기를 넣으면 쓰레기가 나온다는 말입니다.


말도 안 되는 입력 데이터를 학습시켜도 컴퓨터는 이를 처리하며, 말도 안되는 결과를 출력합니다.


물론, 전략이 잘못되었을 수도 있습니다만, 주가를 예측하기가 쉽지 않아 보입니다.


별개로, 흉부 x-ray 이미지를 이용한 폐렴 예측을 해보았더니, 결과가 아주 좋았습니다.


주가 데이터만 도려내고, 필기체나 x-ray 등 다른 이미지 데이터를 이식하면 좋은 결과가 있으리라 믿습니다.


3번의 포스팅을 마무리하며 CNN를 공부하시고 실습해보시는 많은 분들께 도움이 되었으면 좋겠습니다.

부족한 글 읽어주셔서 감사합니다.


비난이 아닌 비판은 환영입니다. 아주 작은 오타라도 혹은 지적을 주시면 감사하겠습니다.

반응형