Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Make a plot for acutal vs predicted : import yfinance as yf import pandas as pd import…

  1. User

    Make a plot for acutal vs predicted : 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 from keras.callbacks import LambdaCallback from keras.regularizers import L1L2 from sklearn.model_selection import TimeSeriesSplit

    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 = 1

    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]

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

    model = Sequential() model.add(LSTM(50, input_shape=(1, look_back), kernel_regularizer=L1L2(l1=0.0, l2=0.1))) model.add(Dense(1))

    2. Regularization

    model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train_cv, y_train_cv, epochs=5, batch_size=1, verbose=0, 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])

  2. Assistant

    Plotting actual vs predicted

    import matplotlib.pyplot as plt

    Shift train predictions for plotting

    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 testPredictPlot[len(trainPredict)+(look_back*2)+1: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()

  3. User

    ValueError Traceback (most recent call last) in <cell line: 13>() 11 testPredictPlot = np.empty_like(dataset) 12 testPredictPlot[:, :] = np.nan ---> 13 testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict 14 15 # Plot baseline and predictions

    ValueError: could not broadcast input array from shape (294,1) into shape (292,1)

  4. Assistant

    It seems like there is a mismatch in the shape of testPredict and the corresponding segment of testPredictPlot. Let's adjust the code to accommodate that and try again:

    # Shift train predictions for plotting
    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
    test_len = len(trainPredict) + (look_back*2) + 1
    testPredictPlot[test_len:test_len+len(testPredict), :] = 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()
    

    Let's see if this resolves the issue.

  5. User

    ValueError Traceback (most recent call last) in <cell line: 10>() 8 testPredictPlot[:, :] = np.nan 9 test_len = len(trainPredict) + (look_back*2) + 1 ---> 10 testPredictPlot[test_len:test_len+len(testPredict), :] = testPredict 11 12 # Plot baseline and predictions

    ValueError: could not broadcast input array from shape (294,1) into shape (293,1)

  6. Assistant

    Apologies for the confusion, let's try a different approach:

    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()
    

    Let's see if this resolves the issue.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.