3 분 소요

!pip install finance-datareader
Collecting finance-datareader
  Downloading finance_datareader-0.9.31-py3-none-any.whl (17 kB)
Collecting requests-file
  Downloading requests_file-1.5.1-py2.py3-none-any.whl (3.7 kB)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from finance-datareader) (4.62.3)
Requirement already satisfied: pandas>=0.19.2 in /usr/local/lib/python3.7/dist-packages (from finance-datareader) (1.3.5)
Requirement already satisfied: lxml in /usr/local/lib/python3.7/dist-packages (from finance-datareader) (4.2.6)
Requirement already satisfied: requests>=2.3.0 in /usr/local/lib/python3.7/dist-packages (from finance-datareader) (2.23.0)
Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.19.2->finance-datareader) (2018.9)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.19.2->finance-datareader) (2.8.2)
Requirement already satisfied: numpy>=1.17.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.19.2->finance-datareader) (1.21.5)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.19.2->finance-datareader) (1.15.0)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.3.0->finance-datareader) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.3.0->finance-datareader) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.3.0->finance-datareader) (2021.10.8)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.3.0->finance-datareader) (2.10)
Installing collected packages: requests-file, finance-datareader
Successfully installed finance-datareader-0.9.31 requests-file-1.5.1
import pandas as pd
import FinanceDataReader as fdr
import matplotlib.pyplot as plt
df = fdr.DataReader('005930','2018-01-01','2022-02-24')   # 삼성전자: 005930
df
Open High Low Close Volume Change
Date
2018-01-02 51380 51400 50780 51020 169485 0.001177
2018-01-03 52540 52560 51420 51620 200270 0.011760
2018-01-04 52120 52180 50640 51080 233909 -0.010461
2018-01-05 51300 52120 51200 52120 189623 0.020360
2018-01-08 52400 52520 51500 52020 167673 -0.001919
... ... ... ... ... ... ...
2022-02-18 74600 74800 73700 74300 10122226 -0.009333
2022-02-21 73200 74300 72600 74200 10489717 -0.001346
2022-02-22 73000 73400 72800 73400 11692469 -0.010782
2022-02-23 73800 73800 72800 73000 10397964 -0.005450
2022-02-24 72300 72300 71300 71500 15502410 -0.020548

1022 rows × 6 columns

data = df['Close'].values

plt.figure(figsize=(12, 8))
plt.plot(data)
plt.show()

from sklearn.preprocessing import MinMaxScaler
import numpy as np
data = np.array(data).reshape(-1,1)    # 표준화를 하기 위해 사이즈를 (-1,1)로 조정

scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(data)
scaled
array([[0.25340803],
       [0.26461251],
       [0.25452848],
       ...,
       [0.6713352 ],
       [0.66386555],
       [0.63585434]])
test_idx = int(len(scaled) * 0.8)

train = scaled[:test_idx]
test = scaled[test_idx:]
x_train = []
y_train = []
x_test = []
y_test = []

pasts = 15

for i in range(pasts, len(train)):
  x_train.append(train[i-pasts:i, 0])
  y_train.append(train[i, 0])

for i in range(pasts, len(test)):
  x_test.append(test[i-pasts:i, 0])
  y_test.append(test[i, 0])
x_train = np.array(x_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
y_train = np.array(y_train)
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
y_test = np.array(y_test)

x_train.shape, y_train.shape, x_test.shape, y_test.shape
((802, 15, 1), (802,), (190, 15, 1), (190,))

keras를 활용한 예측 모델 생성

from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.layers import LSTM

model = Sequential()
model.add(LSTM(50, input_shape=(x_train.shape[1], x_train.shape[2]), activation='relu', return_sequences=True))
model.add(LSTM(10, input_shape=(x_train.shape[1], x_train.shape[2]), activation='relu', return_sequences=False))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit(x_train, y_train, epochs=15, batch_size=16,)
Epoch 1/20
51/51 [==============================] - 5s 13ms/step - loss: 0.0521
Epoch 2/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0042
Epoch 3/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0025
Epoch 4/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0023
Epoch 5/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0019
Epoch 6/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0019
Epoch 7/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0016
Epoch 8/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0014
Epoch 9/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0013
Epoch 10/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0013
Epoch 11/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0013
Epoch 12/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0012
Epoch 13/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0012
Epoch 14/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0012
Epoch 15/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0011
Epoch 16/20
51/51 [==============================] - 1s 12ms/step - loss: 0.0011
Epoch 17/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0010
Epoch 18/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0011
Epoch 19/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0010
Epoch 20/20
51/51 [==============================] - 1s 13ms/step - loss: 0.0011
plt.plot(history.history['loss'])
plt.show()

preds = model.predict(x_test)
plt.plot(preds, label='pred')
plt.plot(y_test, label='label')
plt.legend()
plt.show()


댓글남기기