Search code examples
pythontensorflowkeraslstm

Train model for price prediction


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.


Solution

  • Change this line:

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