1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Why is my Auto ARIMA forecast showing misalignment with actual demand data with clear...

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024 às 21:32.

  1. Stack

    Stack Membro Participativo

    I am using the Auto ARIMA model to forecast demand for a time series dataset. Upon reviewing the results, I noticed that the forecasted values do not align well with the actual demand data, even though I plot the residuals and it is random. Here is the model fitting code:

    import pandas as pd
    import matplotlib.pyplot as plt
    from pmdarima import auto_arima
    from sklearn.metrics import mean_squared_error
    import numpy as np

    # Assuming your DataFrame is 'df', which contains a 'demand' column

    df['demand'] = df['demand'].interpolate(method='linear') # Use interpolation to fill NaN values

    # Set the frequency (adjust as needed: 'D' for daily, 'M' for monthly, etc.)
    df = df.asfreq('D')

    # Split the dataset into train and test sets (80% training, 20% testing)
    train_size = int(len(df) * 0.8)
    train, test = df[:train_size], df[train_size:]

    # Use Auto ARIMA to find the best model
    model = auto_arima(train['demand'],
    seasonal=True, # If seasonality is present
    m=12, # Set to 7 for weekly seasonality (if daily data)
    d=1, # Set non-seasonal differencing
    D=1, # Set seasonal differencing
    trace=True, # Prints the progress
    suppress_warnings=True,
    stepwise=True) # Use a stepwise search


    # Print the best model summary
    print(model.summary())


    The plotting code:

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.metrics import mean_squared_error

    # Calculate RMSE
    rmse = np.sqrt(mean_squared_error(test['demand'], forecast))

    # Create the plot
    plt.figure(figsize=(10, 6))

    # Plot the test (actual) demand
    plt.plot(test.index, test['demand'], label='Actual Demand', color='blue')

    # Plot the forecasted demand
    plt.plot(forecast_index, forecast, label='Forecasted Demand', color='red')

    # Add labels and title
    plt.title('Actual vs Forecasted Demand (Zoomed In)')
    plt.xlabel('Date')
    plt.ylabel('Demand')
    plt.legend()

    # Display RMSE on the plot
    plt.text(forecast_index[-1], test['demand'].max(), f'RMSE: {rmse:.2f}', fontsize=12, ha='right', color='black')

    plt.show()

    # Print the forecasted values for inspection
    print(forecast)



    The results plot:

    enter image description here

    I tried to change the m parameter in the auto ARIMA with different values but it was fruitful!

    Continue reading...

Compartilhe esta Página