I came up with a project:
I want to perform a prediction of stock closing values using an LSTM model. Specific plan: I want the AI to forecast the closing values for the next 90 days, which will also be displayed in a graph. I also want an evaluation system that evaluates the AI based on its accuracy in making forecasts.
For this task I have the following code:
!pip install yfinance import pandas as pd import pandas_datareader as pdr import numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.layers import Dropout from sklearn.metrics import mean_squared_error import yfinance as yf # Step 1: Collect stock data for Adobe Systems Incorporated (ADBE) from Yahoo Finance df = yf.download('ADBE', start="2000-01-01", end="2030-02-05") # Step 2: Preprocess the data # Drop the Adj Close column, as we don't need it for the prediction df.drop(['Adj Close'], axis=1, inplace=True) # Normalize the data scaler = MinMaxScaler(feature_range=(0, 1)) df = scaler.fit_transform(df) # Split the data into training and test sets training_data_len = int(len(df) * 0.8) training_data = df[0:training_data_len, :] test_data = df[training_data_len:, :] # Reshape the data into a format suitable for use with an LSTM def create_training_data(training_data, look_back): X_train, y_train = ,  for i in range(len(training_data) - look_back - 1): a = training_data[i:(i + look_back), :] X_train.append(a) y_train.append(training_data[i + look_back, 0]) return np.array(X_train), np.array(y_train) look_back = 30 X_train, y_train = create_training_data(training_data, look_back) X_train = np.reshape(X_train, (X_train.shape, X_train.shape, 1) # Step 3: Build the LSTM model model = Sequential()model.add(LSTM(50, return_sequences=True, input_shape=(X_train.shape, 1))) model.add(Dropout(0.2)) model.add(LSTM(50, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(50)) model.add(Dropout(0.2)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') # Step 4: Train the LSTM model model.fit(X_train, y_train, epochs=1, batch_size=32) # Step 5: Use the trained LSTM model def create_test_data(test_data, look_back): X_test =  for i in range(len(test_data) - look_back - 1): a = test_data[i:(i + look_back), :] X_test.append(a) X_test = np.array(X_test) X_test = np.reshape(X_test, (X_test.shape, X_test.shape, 5)) predictions = model.predict(X_test) predictions = scaler.inverse_transform(predictions) y_test = test_data[look_back:, 0] y_test = scaler.inverse_transform(y_test.reshape(-1, 1)) rmse = np.sqrt(mean_squared_error(y_test, predictions)) print('Test RMSE: %.3f' % rmse) # Step 6: Use the trained LSTM model to make predictions for the next 90 days df2 = pdr.DataReader('ADBE', data_source='yahoo', start='2023-02-06', end='2023-05-06') last_price = df2['Close'][-1] prediction_dates = pd.date_range(start='2023-02-06', end='2023-05-06', freq='D') df2 = pd.DataFrame(index=prediction_dates, columns=['Prediction']) df2['Prediction'] = last_price for i in range(90): df2['Close'] = df2['Prediction'] df2['Close'] = scaler.transform(df2[['Close']]) X_test = df2[i:i + look_back][['Close']] X_test = np.array(X_test) X_test = np.reshape(X_test, (1, look_back, 1)) prediction = model.predict(X_test) prediction = scaler.inverse_transform(prediction) df2['Prediction'][i + look_back] = prediction # Step 7: Plot the predicted closing values for the next 90 days plt.figure(figsize=(15, 8)) plt.plot(df2['Prediction']) plt.xlabel('Date') plt.ylabel('Closing Value') plt.title('Adobe Systems Inc. (ADBE) Predicted Closing Values for the Next 90 Days') plt.show()
The compiler is giving some errors. Can someone watch this code through and modify it, so that it will work or provide info on how to improve the code? I’m working on it for a week now and its frustrating. There are plenty of synatx error, I just thought that their will be some pros here that could help me out.
Thank you very much in advance.