Last active
March 16, 2020 05:26
-
-
Save Lay4U/2e1759a0e435ff95b7a017e301db634f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from keras.models import Sequential | |
from keras.layers import Dense, LSTM, Dropout, Conv2D, Reshape, TimeDistributed, Flatten, Conv1D,ConvLSTM2D, MaxPooling1D | |
from keras.layers.core import Dense, Activation, Dropout | |
from sklearn.preprocessing import MinMaxScaler | |
from sklearn.metrics import mean_squared_error | |
import tensorflow as tf | |
import matplotlib.pyplot as plt | |
config = tf.ConfigProto() | |
config.gpu_options.allow_growth=True | |
sess = tf.Session(config=config) | |
def create_dataset(signal_data, look_back=1): | |
dataX, dataY = [], [] | |
for i in range(len(signal_data) - look_back): | |
dataX.append(signal_data[i:(i + look_back), 0]) | |
dataY.append(signal_data[i + look_back, 0]) | |
return np.array(dataX), np.array(dataY) | |
forecast = 50 | |
look_back = 20 | |
#kospi.csv is https://docs.google.com/spreadsheets/d/13qyMDbl9EsBPE6asoXkH_73Y4QVGzaiUXyir94nN3VE/edit?usp=sharing | |
df = pd.read_csv('kospi.csv') | |
signal_data = df.Close.values.astype('float32') | |
total_data = df.Close.values.astype('float32') | |
signal_data = signal_data.reshape(len(df), 1) | |
scaler = MinMaxScaler(feature_range=(0, 1)) | |
signal_data = scaler.fit_transform(signal_data) | |
train_size = int(len(signal_data) * 0.80) | |
test_size = len(signal_data) - train_size - int(len(signal_data) * 0.05) | |
val_size = len(signal_data) - train_size - test_size | |
train = signal_data[0:train_size] | |
val = signal_data[train_size:train_size+val_size] | |
test = signal_data[train_size+val_size:len(signal_data)] | |
x_train, y_train = create_dataset(train, look_back) | |
x_val, y_val = create_dataset(val, look_back) | |
x_test, y_test = create_dataset(test, look_back) | |
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) | |
x_val = np.reshape(x_val, (x_val.shape[0], x_val.shape[1], 1)) | |
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1)) | |
model = Sequential() | |
model.add(LSTM(128, input_shape=(None, 1),return_sequences=True)) | |
model.add(Dropout(0.3)) | |
model.add(LSTM(128, input_shape=(None, 1))) | |
model.add(Dropout(0.3)) | |
model.add(Dense(128)) | |
model.add(Dropout(0.3)) | |
model.add(Dense(1)) | |
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) | |
model.summary() | |
hist = model.fit(x_train, y_train, epochs=20, batch_size=32, verbose=2, validation_data=(x_val, y_val)) | |
trainScore = model.evaluate(x_train, y_train, verbose=0) | |
model.reset_states() | |
print('Train Score: ', trainScore) | |
valScore = model.evaluate(x_val, y_val, verbose=0) | |
model.reset_states() | |
print('Validataion Score: ', valScore) | |
testScore = model.evaluate(x_test, y_test, verbose=0) | |
model.reset_states() | |
print('Test Score: ', testScore) | |
inputs = total_data[len(total_data) - forecast - look_back:] | |
inputs = scaler.transform(inputs) | |
X_test = [] | |
for i in range(look_back, inputs.shape[0]): | |
X_test.append(inputs[i - look_back:i]) | |
X_test = np.array(X_test) | |
predicted = model.predict(X_test) | |
#kospi.csv is https://docs.google.com/spreadsheets/d/13qyMDbl9EsBPE6asoXkH_73Y4QVGzaiUXyir94nN3VE/edit?usp=sharing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment