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

Create an expert level lengthy advanced script in python for Use 2 layers of LSTM to…

  1. User

    Create an expert level lengthy advanced script in python for Use 2 layers of LSTM to predict next hour’s price instead of next day’s price which may have better application in real world.


    First you will implemented data normalization like min-max normalization and normalization

    with window [5] where the data is normalized based on the window’s initial value and the percentage of change.


    Multiple Layer Perceptron (MLP), Long-Short-

    Term-Memory (LSTM) and Gated recurrent units (GRU) models are compared

    on the test dataset with cross-validation.

    1. Dataset Exploration

    Data used in this research is collected from Kaggle [6]. Bitcoin data from Jan

    2016 to July 2022 is collected. It has a timestamp, the value at Open, High, Low,

    Close, the volume traded in Bitcoin and USD, the weighted price and the date.

    This prompt focuses on predicting Bitcoin price in the future hour by using the price of past 24 hours, so only the timestamp and the

  2. Assistant

    weighted price are of our interest. The weighted price is the average price of Bitcoin for a specific period of time.

    Below is the implementation of the mentioned concepts.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from sklearn.preprocessing import MinMaxScaler
    from sklearn.metrics import mean_squared_error
    from keras.models import Sequential
    from keras.layers import Dense, LSTM, Dropout
    from keras.callbacks import EarlyStopping
    
    # Load the dataset
    data = pd.read_csv('bitcoin_price.csv', usecols=['Timestamp', 'Weighted_Price'])
    data['Timestamp'] = pd.to_datetime(data['Timestamp'], unit='s')
    data = data.set_index('Timestamp')
    
    # Data normalization using MinMaxScaler
    scaler = MinMaxScaler(feature_range=(0, 1))
    data['Weighted_Price'] = scaler.fit_transform(data[['Weighted_Price']])
    
    # Data normalization using a window of size 5
    window_size = 5
    data['Normalized_Window_Price'] = data['Weighted_Price'].pct_change(window_size)
    data = data.dropna()
    
    # Train-test split keeping the last 10% for testing
    train_size = int(len(data) * 0.9)
    train, test = data[0:train_size], data[train_size:len(data)]
    
    # Prepare the data for LSTM
    def create_dataset(dataset, lookback=24):
        data_x, data_y = [], []
        for i in range(len(dataset) - lookback):
            data_x.append(dataset.iloc[i:i + lookback].values)
            data_y.append(dataset.iloc[i + lookback].values)
        return np.array(data_x), np.array(data_y)
    
    lookback = 24
    train_x, train_y = create_dataset(train, lookback)
    test_x, test_y = create_dataset(test, lookback)
    
    train_y = train_y[:, 0]
    test_y = test_y[:, 0]
    
    # Reshape the input to be [samples, timesteps, features]
    train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
    test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1))
    
    # Define the LSTM model with 2 layers and dropout for regularization
    model = Sequential()
    model.add(LSTM(50, return_sequences=True, input_shape=(train_x.shape[1], train_x.shape[2])))
    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')
    
    # Training the model
    early_stopping = EarlyStopping(monitor='val_loss', patience=10)
    history = model.fit(train_x, train_y, epochs=100, batch_size=32, validation_split=0.1, callbacks=[early_stopping], shuffle=False)
    
    # Plotting the loss
    plt.plot(history.history['loss'], label='train')
    plt.plot(history.history['val_loss'], label='validation')
    plt.legend()
    plt.show()
    
    # Predicting the next hour's price
    pred_y = model.predict(test_x)
    
    # Invert predictions
    pred_y = scaler.inverse_transform(pred_y)
    test_y = scaler.inverse_transform(test_y.reshape(-1, 1))
    
    # Calculate the root mean squared error
    rmse = np.sqrt(mean_squared_error(test_y, pred_y))
    print("RMSE: {:.3f}".format(rmse))
    
    # Plot the predictions
    plt.plot(test_y, label='Actual')
    plt.plot(pred_y, label='Predicted')
    plt.legend()
    plt.xlabel("Hour")
    plt.ylabel("Bitcoin Weighted Price")
    plt.show()
    

    This script implements data normalization using MinMaxScaler and windows normalization, train-test split, and an LSTM model with 2 layers to predict the next hour's Bitcoin price. It also calculates the root mean squared error and visualizes the training and validation loss during training, as well as the predicted and actual prices.