Train model for price prediction

Viewed 85

I'm trying to make stock predictor, i know that model which i will create, won't be great, but nonetheless. First off below i will add code that formate X and Y. X contains stocks prices for 10 days, Y contains answers(prices for next days).

import yfinance as yf
import numpy as np
import pandas as pd
import json
from sys import exit
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dropout
from keras.layers import Dense 



with open('/content/drive/MyDrive/tickers.json', 'r', encoding='utf-8') as f:
  tickers_list = eval(json.loads(f.read()))['tickers'][:100] # list of tickers


X = []
Y = []


for ticker in tickers_list:
   stock = yf.download(ticker,'1990-01-01','2019-12-31')['Adj Close'].values
   for i in range(len(stock)):
     try:
       stock[i+11]
     except:
       continue
     X.append(stock[i:i+10])
     Y.append([stock[i+11]])
X = np.array(X) #price for 10 days
Y = np.array(Y) #price for next day

After that i wrote code for creating model.

X_train = np.expand_dims(X, 1)

model = Sequential()
model.add(LSTM(units=64,return_sequences=True, input_shape=(10, 1)))
model.add(Dropout(0.2))
model.add(LSTM(units=64,return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=64,return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=64))
model.add(Dropout(0.2))
model.add(Dense(units=1))
model.compile(optimizer='adam',loss='mean_squared_error')

print(model.summary())

model.fit(X_train, Y, epochs=100, batch_size=32)

But when i ran code above, i got error.

Input 0 is incompatible with layer sequential_17: expected shape=(None, None, 1), found shape=[None, 1, 10]

How to fix it? Thank u.

Note If u have any suggestions that could make my research better, pls write me here.

2 Answers

Change this line:

X_train = np.expand_dims(X, 2) # 2 instead of 1

I suggest you use this function to create a time series dataset. Yours, including the error handling, seems suboptimal. This function is coming from a past version of the official Tensorflow documentation. Besides, it seems like you are concatenating a bunch of time series one after the other, which makes no sense.

def univariate_data(dataset, start_index, end_index, history_size,
                    target_size, single_step=False):
    data, labels = [], []
    start_index = start_index + history_size
    if end_index is None:
        end_index = len(dataset) - target_size
    for i in range(start_index, end_index):
        indices = np.arange(i-history_size, i)
        data.append(np.reshape(dataset[indices], (history_size, 1)))
        if single_step:
            labels.append(dataset[i + target_size])
        else:
            labels.append(dataset[i:i + target_size])
    return np.array(data), np.array(labels)

Then, you won't even face the problem you're having. On top of that, please note that you do not need to print model.summary(), calling this method automatically prints it. Here's a full working example with the function I provided:

import yfinance as yf
import numpy as np
import tensorflow as tf

stock = yf.download('aapl','1990-01-01','2019-12-31')['Adj Close'].values

def univariate_data(dataset, start_index, end_index, history_size,
                    target_size, single_step=False):
    data, labels = [], []
    start_index = start_index + history_size
    if end_index is None:
        end_index = len(dataset) - target_size
    for i in range(start_index, end_index):
        indices = np.arange(i-history_size, i)
        data.append(np.reshape(dataset[indices], (history_size, 1)))
        if single_step:
            labels.append(dataset[i + target_size])
        else:
            labels.append(dataset[i:i + target_size])
    return np.array(data), np.array(labels)

X, y = univariate_data(stock, 0, len(stock) - 1, 10, 1, True)

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units=8,return_sequences=True,
                               input_shape=(10, 1)))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units=8,return_sequences=True))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units=8,return_sequences=True))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units=8))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(units=1))
model.compile(optimizer='adam',loss='mean_squared_error')

model.summary()

history = model.fit(X, y, steps_per_epoch=1)
7547/7547 [==============================] - 12s 2ms/sample - loss: 286.5788
Related