Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

Use a combination of lstm and CNN model for stock market prediction current value of…

  1. User

    Use a combination of lstm and CNN model for stock market prediction current value of rmse is more than 1000 And this model has made a prediction for 2021-01-02 is -5000 which is not possible please correct this model improve it so that model is more relable:

    Code: import yfinance as yf import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM, Dropout from keras.regularizers import L1L2 from sklearn.model_selection import TimeSeriesSplit import matplotlib.pyplot as plt

    nifty = yf.download('^NSEI', start='2009-01-01', end='2021-01-01')

    def supertrend(df, period=10, multiplier=3): hl = (df['High'] + df['Low']) / 2 atr = df['High'].rolling(period).max() - df['Low'].rolling(period).min()

    up = hl - multiplier * atr
    dn = hl + multiplier * atr
    
    df['ST'] = 0
    df['ST'][0] = (df['High'][0] + df['Low'][0]) / 2
    
    position = 'none'
    
    for i in range(1, len(df)):
        if df['Close'][i] > up[i]:
            if position != 'buy':
                position = 'buy'
                df['ST'][i] = dn[i]
            else:
                df['ST'][i] = max(df['ST'][i - 1], dn[i])
        elif df['Close'][i] < dn[i]:
            if position != 'sell':
                position = 'sell'
                df['ST'][i] = up[i]
            else:
                df['ST'][i] = min(df['ST'][i - 1], up[i])
        else:
            df['ST'][i] = df['ST'][i - 1]
    
    return df
    

    nifty = supertrend(nifty) nifty.head()

    dataset = nifty['Close'].values dataset = dataset.astype('float32') dataset = dataset.reshape(-1, 1) scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset)

    def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY)

    look_back = 100

    X, y = create_dataset(dataset, look_back)

    X = np.reshape(X, (X.shape[0], 1, X.shape[1]))

    1. Increasing the size of training data

    train_size = int(len(X) * 0.9) test_size = len(X) - train_size X_train, X_test = X[:train_size], X[train_size:] y_train, y_test = y[:train_size], y[train_size:]

    4. Cross-validation

    tscv = TimeSeriesSplit(n_splits=5)

    for train_index, test_index in tscv.split(X_train): X_train_cv, X_valid_cv = X_train[train_index], X_train[test_index] y_train_cv, y_valid_cv = y_train[train_index], y_train[test_index]

    from sklearn.linear_model import Lasso from sklearn.feature_selection import SelectFromModel

    lasso = Lasso(alpha=0.1) selector = SelectFromModel(lasso) selector.fit(X, y) features_selected = selector.get_support(indices=True)

    3. Simplifying the model -reduce layers or use smaller layers

    model = Sequential() model.add(LSTM(units=100, input_shape=(1, look_back), return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(units=50, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(units=50, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(units=50)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam', learning_rate=0.001) model.fit(X_train_cv, y_train_cv, epochs=200, batch_size=1, verbose=1, validation_data=(X_valid_cv, y_valid_cv))

    trainPredict = model.predict(X_train) testPredict = model.predict(X_test)

    trainPredict = scaler.inverse_transform(trainPredict) y_train = scaler.inverse_transform([y_train]) testPredict = scaler.inverse_transform(testPredict) y_test = scaler.inverse_transform([y_test])

    trainPredictPlot = np.empty_like(dataset) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict

    Shift test predictions for plotting

    testPredictPlot = np.empty_like(dataset) testPredictPlot[:, :] = np.nan total_len = len(dataset) - len(testPredict) - 1 testPredictPlot[total_len:len(dataset)-1, :] = testPredict

    Plot baseline and predictions

    plt.figure(figsize=(12,6)) plt.plot(scaler.inverse_transform(dataset), label='Actual') plt.plot(trainPredictPlot, label='Train Prediction') plt.plot(testPredictPlot, label='Test Prediction') plt.xlabel('Time Index') plt.ylabel('Stock Price') plt.legend(loc='upper left') plt.title('Actual vs Predicted Stock Prices')

    plt.show()

    model.save('my_model.h5')

    from sklearn.metrics import mean_squared_error import math

    trainScore = math.sqrt(mean_squared_error(y_train[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = math.sqrt(mean_squared_error(y_test[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore))

    last_day = dataset[-look_back:]

    last_day_scaled = scaler.transform(last_day)

    3. Reshape the input to match the required model input shape

    last_day_scaled_reshaped = np.reshape(last_day_scaled, (1, 1, look_back))

    4. Use model.predict() to predict the scaled value for ‘2021-01-02’

    next_day_scaled_prediction = model.predict(last_day_scaled_reshaped)

    5. Inverse-transform the predicted scaled value to the original range using the MinMaxScaler

    next_day_prediction = scaler.inverse_transform(next_day_scaled_prediction)

    6. Print or use the predicted value

    print("Prediction for 2021-01-02: ", next_day_prediction[0][0])

  2. Assistant

    In the existing model, there are a few issues that could be causing the predicted value for 2021-01-02 to be inaccurate:

    1. The model might not be generalizing well on the test data due to overfitting during training.
    2. The combination of LSTM and CNN may not be the optimal architecture for this problem.
    3. The LSTM layer's hyperparameters may not be ideal.

    To address these issues, I suggest making the following changes:

    1. Reduce overfitting by using regularization in LSTM layers.
    2. Remove CNN layers and focus on LSTM layers.
    3. Optimize LSTM layer hyperparameters such as the number of layers, units, and dropout.

    Here's an updated version of the code with these changes:

    import yfinance as yf
    import pandas as pd
    import numpy as np
    from sklearn.preprocessing import MinMaxScaler
    from keras.models import Sequential
    from keras.layers import Dense, LSTM, Dropout
    from keras.regularizers import L1L2
    from sklearn.model_selection import TimeSeriesSplit
    import matplotlib.pyplot as plt
    
    # 1. Reduce overfitting by using regularization in LSTM layers
    reg = L1L2(l1=0.01, l2=0.01)
    
    model = Sequential()
    model.add(LSTM(units=100, activation='relu', kernel_regularizer=reg, input_shape=(1, look_back), return_sequences=True))
    model.add(Dropout(0.2))
    # 3. Optimize LSTM layer hyperparameters
    model.add(LSTM(units=50, activation='relu', kernel_regularizer=reg, return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(units=50, activation='relu', kernel_regularizer=reg, return_sequences=False))
    model.add(Dropout(0.2))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam', learning_rate=0.001)
    model.fit(X_train_cv, y_train_cv, epochs=200, batch_size=1, verbose=1, validation_data=(X_valid_cv, y_valid_cv))
    
    # Remove unused Lasso code
    # from sklearn.linear_model import Lasso
    # from sklearn.feature_selection import SelectFromModel
    
    # lasso = Lasso(alpha=0.1)
    # selector = SelectFromModel(lasso)
    # selector.fit(X, y)
    # features_selected = selector.get_support(indices=True)
    
    # Evaluate the updated model
    trainScore = math.sqrt(mean_squared_error(y_train[0], trainPredict[:,0]))
    print('Train Score: %.2f RMSE' % (trainScore))
    testScore = math.sqrt(mean_squared_error(y_test[0], testPredict[:,0]))
    print('Test Score: %.2f RMSE' % (testScore))
    
    # Make the prediction for 2021-01-02
    last_day = dataset[-look_back:]
    last_day_scaled = scaler.transform(last_day)
    last_day_scaled_reshaped = np.reshape(last_day_scaled, (1, 1, look_back))
    next_day_scaled_prediction = model.predict(last_day_scaled_reshaped)
    next_day_prediction = scaler.inverse_transform(next_day_scaled_prediction)
    print("Prediction for 2021-01-02: ", next_day_prediction[0][0])
    

    By making these changes, the model's RMSE should decrease, and the prediction for 2021-01-02 should become more reliable. Please note that these changes are just suggestions and may require further fine-tuning to achieve optimal results. It is also possible that LSTM may not be the best model for this problem, and you may need to explore other models such as ARIMA, GARCH or machine learning approaches like decision trees and neural networks.