Skip to content

Instantly share code, notes, and snippets.

@alik604
Created July 28, 2020 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alik604/69548f99a9dda2d45c42db59e226cff0 to your computer and use it in GitHub Desktop.
Save alik604/69548f99a9dda2d45c42db59e226cff0 to your computer and use it in GitHub Desktop.
build_dataset LSTM.py
# make dataset to input
def build_dataset(time_series, seq_length):
dataX = []
dataY = []
for i in range(0, len(time_series) - seq_length):
_x = time_series[i:i + seq_length, :]
_y = time_series[i + seq_length, [-1]] # Next close price
print(_x, "->", _y)
dataX.append(_x)
dataY.append(_y)
return np.array(dataX), np.array(dataY)
@alik604
Copy link
Author

alik604 commented Jul 28, 2020

train= scaled_data[:int(df.shape[0]*0.8)]
valid = scaled_data[int(df.shape[0]*0.8):]

x_train,y_train,x_test,y_test = [],[],[],[]
for i in range(60,train.shape[0]):
    x_train.append(train[i-60:i,0])
    y_train.append(train[i,0])

for z in range(60,valid.shape[0]):
    x_test.append(valid[z-60:z,0])
    y_test.append(valid[z,0])

x_train, y_train,x_test,y_test = np.array(x_train), np.array(y_train),np.array(x_test),np.array(y_test)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))
x_test=np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))

@alik604
Copy link
Author

alik604 commented Aug 17, 2020

src

import numpy as np

def to_sequences(seq_size, obs):
    x = []
    y = []

    for i in range(len(obs)-SEQUENCE_SIZE-1):
        #print(i)
        window = obs[i:(i+SEQUENCE_SIZE)]
        after_window = obs[i+SEQUENCE_SIZE]
        window = [[x] for x in window]
        #print("{} - {}".format(window,after_window))
        x.append(window)
        y.append(after_window)
        
    return np.array(x),np.array(y)
    
    
SEQUENCE_SIZE = 25
x_train,y_train = to_sequences(SEQUENCE_SIZE,spots_train)
x_test,y_test = to_sequences(SEQUENCE_SIZE,spots_test)

print("Shape of training set: {}".format(x_train.shape))
print("Shape of test set: {}".format(x_test.shape))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment