Search code examples
pythonmachine-learningstatsmodelsarima

ValueError: too many values to unpack (expected 3) in Python ARIMA implementation


I am trying to create an ARIMA model to predict stock market values (experimenting with this, not going to step on it to use it real life) and export it in PNG format for all the 512 stocks i have in my dataset. In the PNG it will be shown the Actual and the predicted value.

Error:

forecast, stderr, conf_int = model_fit.forecast(steps=len(X_test))   
ValueError: too many values to unpack (expected 3)
import os
import pandas as pd
from statsmodels.tsa.arima.model import ARIMA

# Load Training and Test Data.
train_df = pd.read_csv(r'mypath\train.csv', parse_dates=['datetime'])
test_df = pd.read_csv(r'mypath\test.csv', parse_dates=['datetime'])

# Getting unique stock names.
stocks = train_df['Short Company Name'].unique()

# Create a directory to save pngs.
output_directory = r'ARIMAprediction'
os.makedirs(output_directory, exist_ok=True)

for stock in stocks:
    print(f"Processing Stock: {stock}")

    # Filter data for the current stock.
    train_stock = train_df[train_df['Short Company Name'] == stock]
    test_stock = test_df[test_df['Short Company Name'] == stock]

    # Extracting features (for this, all the dataset is used).
    features = ['open', 'high', 'low', 'close', 'volume']
    X_train, y_train = train_stock[features], train_stock['close']
    X_test, y_test = test_stock[features], test_stock['close']

    # Fit ARIMA model
    order = (5, 1, 2)  
    model = ARIMA(y_train, order=order)
    model_fit = model.fit()

    # Prediction.
    forecast, stderr, conf_int = model_fit.forecast(steps=len(X_test))

Solution

  • The forecast method of your model_fit ARIMAResults object returns an array_like of length steps. Which is why it return too many values and can't unpack them into three variables.

    To get confidence intervals you need to use the get_forecast method instead. statsmodel has some examples in their documentation that seem close to what you are trying to do. There is a small section on the difference between forecast and get_forecast. There is also a section showing how to plot the forecast and it's confidence interval with matplotlib.